Uninitialized variables in PHP

The PHP manual states :

There is no need to initialize variables in PHP, but it is a very good practice. Uninitialized variables have a default value; their type depends on the context in which they are used - boolean is FALSE by default, integers and floats are zero by default, strings (for example, used in echo) are set as an empty string, and arrays become an empty array.

I played with uninitialized golf variables, but the program did not do what I expected from it. After studying, I noticed this strange behavior (all used variables are uninitialized):

php > $a = $a + 1;
PHP Notice:  Undefined variable: a in php shell code on line 1
php > $b = $b - 1;
PHP Notice:  Undefined variable: b in php shell code on line 1
php > $c++;
PHP Notice:  Undefined variable: c in php shell code on line 1
php > $d--;
PHP Notice:  Undefined variable: d in php shell code on line 1
php > var_dump($a);
int(1)
php > var_dump($b);
int(-1)
php > var_dump($c);
int(1)
php > var_dump($d);
NULL

+ 1, - 1and ++work as described in the manual. However --does not work.

$a, $b $c , . $d--; $d, $d - NULL.

$d NULL, -1?

: btw: 1 ++$v;, NULL --$v;.

+4
1

manual:

:... NULL , 1.

, NULL. 1 ( NULL + 1). , .

, .

, . , , : .

+1

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


All Articles