Characteristics of static local variables:
1. It will not change with the call and exit of the function, but although the variable continues to exist, it cannot be used. If the function that defines it is called again, it can continue to be used and saves the value left after the previous call.
2. Static local variables will only be initialized once
3. Static properties can only be initialized as a character value or a constant, and expressions cannot be used. Even if the initial value is not assigned when the local static variable is defined, the system will automatically assign the initial value 0 (logical variable) or null characters (logical variables); the initial value of the static variable is 0.
4. When a function is called multiple times and the values of certain variables are required to be retained between calls, static local variables can be considered. Although the above goals can be achieved with global variables, global variables can sometimes cause unexpected side effects, so it is still advisable to use local static variables.
function test()
{
static $var = 5; //static $var = 1+1; will report an error
$var++;
echo $var . ' ';
}
test(); //2
test(); //3
test(); //4
echo $var; //Report an error: Notice: Undefined variable: var
About static global variables:
//Global variables are static storage methods, and all global variables are static variables
function static_global(){
global $glo;
$glo++;
echo $glo.'<br>';
}
static_global(); //1
static_global(); //2
static_global(); //3
echo $glo . '<br>'; //3
Therefore, static global variables are not used much.