Why is the second static assignment of variables not the first to take effect?

function track_times() { static $i = 0; $i++; static $i = 5; return $i; } echo track_times() . "\n"; echo track_times() . "\n"; 

Result:

 6 7 

I know that people do not use static variables in this way, they simply cannot explain the result. The result implies that the second assignment takes effect, but $i increases to the destination, so why does the first function call return 6?

+6
source share
1 answer

Static declarations are allowed at compile time. You increase it at runtime. Therefore, you increase it after it is already declared as 5. See also http://www.php.net/manual/en/language.variables.scope.php

+3
source

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


All Articles