What if I want to know how many times the function was called? There is no good way to solve a static variable without learning it.
Static variables are characterized by the declaration of a static variable, the second time the function is called, the static variable will no longer initialize the variable, will be read execution on the basis of the original value.
Try the demo () function 10 times and then try the test () function 10 times:
<?php
function demo(){
$a = 0;
echo $a;
$a++;
}
?>
<?php
function test(){
static $a = 0;
echo $a;
$a++;
}
?>
demo();
demo();
demo();
demo();
demo();
demo();
demo();
demo();
demo();
demo();
/*
for($i = 0 ;$i < 10 ; $i++){
test();
}
*/
In the example above you will find:
Test (), the execution of the value will be added 1, and the demo output display results, always 0.
By using the example above you will find the characteristics of static variables described at the beginning of this article.
From for notes (Wiz)
Frontend PHP entry -016-static variables