Study and analysis of the static keyword principle in star alliance PHP

WBOY
Release: 2016-07-29 08:46:12
Original
854 people have browsed it

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--;
}
?>


The execution result is as follows:
This is a recursive function, declared static The variable count records the number of times and outputs 1~10.
I had a doubt when I was looking at it. When calling recursively, the static $count = 0; statement will be executed repeatedly. Why doesn't this cause the count variable to be assigned repeatedly? I studied this question with my colleagues and the test code is as follows:

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

  1. 4
  2. Notice: Undefined variable: a
  3. 4
  4. 10
  5. 10 defined variable: a
  6. 10
  7. 10
  8. 100
  9. 100
  10. 100
  11. end
(The part about the file location in the result has been deleted. You can also remove the echo statement and use zend's debug function to view the result. More clearly)

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.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template