Hello.
Tell me, please, why this code does not issue NOTICE?
error_reporting(E_ALL); $v = NULL; echo $v[0]; Accordingly, if $ v is not declared as NULL, then there will be a Notice: Undefined variable: v in
Thank.
Hello.
Tell me, please, why this code does not issue NOTICE?
error_reporting(E_ALL); $v = NULL; echo $v[0]; Accordingly, if $ v is not declared as NULL, then there will be a Notice: Undefined variable: v in
Thank.
The answer was quite simple. When PHP doesn't like something, it converts the type. At what to understand, when this transformation on the fly is valid, and when not valid, it seems impossible. In short:
Converting NULL to an array results in an empty array.
http://php.net/manual/en/language.types.array.php
and another funny line from the same:
Null will be the keyed string.
When trying to set an index, PHP looks at a variable, finds NULL , considers it to be equivalent to an array , converts it to an empty array, and sets a value. The notification does not appear, because according to the developers philosophy, NULL is identical to the array . But they could notch.
Evidence:
$a = null; $a[0] = 'What the hell?'; var_dump(null == array(), $a == null, gettype($a), $a); /* bool(true) bool(false) string(5) "array" array(1) { [0] => string(14) "What the hell?" } */ ps although it is still not clear why access to a nonexistent index is not accompanied by a notification.
pps uiiiiiiiiiiiiiiiiiiii
$varA = null; for ($i = 0; $i < 999; $i++) { $varA = $varA[$i]; // $varB = null[$i]; // вот заметьте, здесь он ругнется на невалидный синтаксис } var_dump($varA /*, $varB*/); Source: https://ru.stackoverflow.com/questions/359075/
All Articles