PHP: static variable outside of class

php, static

Solution

From the manual:

Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

<?php
function test()
{
     static $a = 0;
     echo $a;
     $a++;
}
?>

Now, $a is initialized only in first call of function and every time the test() function is called it will print the value of $a and increment it.

Problem

I have seen in 3rd party code a variable declared as static, but outside of any class, simply in a "normal" function. ``` <?php function doStuff(){ static $something = null; } ?> ``` I have never seen `static` used this way, and I cannot find anything it in the PHP documentation. Is this legal PHP code? Is this effectively the same as a global variable? If not, what is the purpose?

Original source

Related problems