Why doesn't handling integers as arrays ($ int [$ index]) cause errors in PHP?

This is just a matter of curiosity. I spent all day debugging my PHP code and found that the problem was handling the whole as an array:

$x = $int[$index]; // this returns null, but no error at all 

The whole was actually meant to be an array, but the function for transmitting the array mixed up and passed the first value in the array. Is there a reason the error is not showing? Is accessing the undefined index in the array an error, but trying to access an nonexistent index for an integer fails?

+7
arrays php error-handling
source share
3 answers

I initially thought; it prints $int to a string because [] is another way to access string positions. It seems plausible that this will not cause an error:

 $string 'Hello'; echo $string[0]; // H 

However, this is not the case:

 $int = 1; echo $int[0]; 

Prints nothing, and var_dump($int[0]) returns NULL .

Interestingly, the same behavior appears for bool and float , and the operation used is FETCH_DIM_R , which represents the value of an element in "index" in "array-value" to save it in the "result" ".

From the Arrays guide:

Note The array dereferences a scalar value that is not a string and gives NULL , i.e. without giving an error message.

Like this phenomenon where an error does not occur. Errors # 68110 and # 54556 :

 $array['a'] = null; echo $array['a']['non-existent']; 

Not surprisingly, the assignment does not work:

 $int = 1; $int[0] = 2; 

Warning: you cannot use a scalar value as an array

Thus, PHP is actually trying to access int , bool , float as an array, but does not generate an error. This is from at least version 4.3.0 to 7.2.2.

+4
source share

Contrary to my original theory that you invoke undefined behavior, this behavior is actually defined in the Array documentation .

Note:
An array dereferencing a scalar value that is not a string silently yields NULL, that is, without generating an error message.

In this case, it seems that no type manipulation occurs at all, so these documentation links to conversion to array are not useful for understanding this.


An explicit conversion to an array is defined .

For any of the integer, float, string, boolean and resource types, converting the value to an array results in an array with one element with index zero and the value of the converted scalar. In other words, the (array) $ scalarValue is exactly the same as the ($ scalarValue) array.

Automatic conversion to an undefined array according to the type of juggling manipulation .

Note:
The behavior of automatic conversion to an array is currently undefined.

At first I thought that this is what happens in this case, but since lettuce indicated that this behavior is described elsewhere, I'm not sure what “automatic conversion to array” is.

As to why this gives you a null value without causing errors, warnings or notifications, this is how the PHP interpreter was implemented, and as far as possible, it is not responsible here. You should ask the developers, and they can tell you.

+1
source share

this is the "juggling type" , the "function" of PHP.

you can read more in the white paper

EDIT According to the documentation, "The behavior of automatic conversion to an array is currently undefined."

I run a quick test, just for curiosity:

 <?php $int = 1; $index = 0; $x = $int[$index]; echo gettype($int[$index]) . PHP_EOL; var_dump($x); 

and the output shows that gettype () returns NULL, so I assume that in this case it will convert to NULL

-2
source share

All Articles