When reading the PHP manual, I found the following piece of code:
Copy code The code is as follows:
function Test()
{
static $count = 0;
$count++;
echo $count;
if ($count < 10) {
Test();
}
$count--;
}
?>
Copy the code The code is as follows:
echo 'start
';
static $a = 10 ;
echo "$a
";
unset($GLOBALS['a']);
echo "$a
";
static $a = 20;
echo "$ a
";
$GLOBALS['a'] = 10;
echo "$a
";
static $a = 30;
echo "$a
unset($GLOBALS['a']);
echo "$a
";
static $a;
echo "$a
";
static $a = 40;
echo "$a
";
$a = 100;
echo "$a
";
static $a = 50;
echo "$a
";
static $a = 4;
echo "$a
";
echo 'end
';
exit;
?>
Execute The results are as follows:
start
The value of line 5 of the code output for the first time
$a is 4. It is speculated that PHP allocates the memory of the static variable when the page is initialized, and the last part of the same variable is used at this time. A declared value (this can be tested by changing 4 to another number). Line 7 of the code calls the unset function to destroy the variable $a. When the value of $a is output again, you will see an undefined variable prompt, indicating that the variable has been destroyed. When line 10 is output again, the output result is still 4 instead of 20. There are two possibilities. One is that PHP initializes the value of $a again, and the other is that PHP uses the value of $a before it is destroyed. , this problem is solved when outputting on line 20. The value of $a is 10 when it is destroyed in line 16, and the output is still 10 after it is declared in line 19.
Change the value of $a to 10 in line 11, declare $a again in line 14, and output as 10 in line 17. It is speculated that when the declaration is repeated, PHP still uses the value in the static variable memory without assigning it again.
At this point, the problem found in the manual has been roughly solved, that is, the statement in the recursive call did not change the value of $count, so the recursion stopped successfully when $count=10.
There may be some incorrect understandings, please feel free to comment.
The above introduces the study, research and analysis of the static keyword principle in star alliance PHP, including the content of star alliance. I hope it will be helpful to friends who are interested in PHP tutorials.