Why, when accessing a null value in PHP as an array, a warning is not generated?

I was struck by an error in which the expected DB result was supposed to be an array, but due to a failure in the SQL query in one instance, the DB wrapper correctly returned null. Unfortunately, the code did not expect this (= error).

However, I was surprised to see that PHP did not give a warning when a null value was inverted as an array. Does anyone know if this is by design or is there an explanation that I am missing?

An example to show what I mean:

$ php -a
Interactive shell

> echo phpversion() . PHP_EOL;
5.4.26

php > echo $test['value'];
PHP Notice:  Undefined variable: test in php shell code on line 1

php > $test = 'string';
php > echo $test['value'];
PHP Warning:  Illegal string offset 'value' in php shell code on line 1

php > $test = null;
php > echo $test['value'];

I expect the last statement to generate a warning.

+4
source share

No one has answered this question yet.

Source: https://habr.com/ru/post/1539556/


All Articles