In PHP, I have error_reporting
set to report everything including notices.
Why does the following not throw any notices, errors or anything else?
$myarray = null; $myvalue = $myarray['banana'];
Troubleshooting steps:
$myarray = array(); $myvalue = $myarray['banana']; // throws a notice, as expected ✔ $myarray = (array)null; $myvalue = $myarray['banana']; // throws a notice, as expected ✔ $myarray = null; $myvalue = $myarray['banana']; // no notice or warning thrown, $myvalue is now NULL. ✘ Why?
It's possible it's a bug in PHP, or I'm just not understanding something about how this works.
An array cannot be null.
PHP NULL ValueNull is a special data type which can have only one value: NULL. A variable of data type NULL is a variable that has no value assigned to it. Tip: If a variable is created without a value, it is automatically assigned a value of NULL.
NULL essentially means a variable has no value assigned to it; false is a valid Boolean value, 0 is a valid integer value, and PHP has some fairly ugly conversions between 0 , "0" , "" , and false . Show activity on this post. Null is nothing, False is a bit, and 0 is (probably) 32 bits.
You can use comparison operator == , === or != to check whether a variable is null or not.
There are three types which it might be valid to use the array derefence syntax on:
For all other types, PHP just returns the undefined variable.
Array dereference is handled by the FETCH_DIM_R opcode, which uses zend_fetch_dimension_address_read() to fetch the element.
As you can see, there is a special case for NULLs, and a default case, both returning the undefined variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With