Brief analysis of the use of php variable modifier static

  • 2020-06-23 00:05:29
  • OfStack

A static variable exists only in a local function domain, but its value is not lost when the program execution leaves this scope. Take a look at this example:

function test(){
static $a=0;
$a++;
echo $a;
}

test();//1
test();//2
test();//3
Note: Static variables can be declared as in the example above. Assigning the result of an expression to a declaration results in a parsing error.

static $a=0+1;
static $a=sqrt(121);

An assignment like the one above will give you an error

Related articles: