Study and analysis on the principle of static keyword in PHP _php skills

Source: Internet
Author: User
Looking at the PHP manual, I found the following code:
Copy Code code as follows:

<?php
function Test ()
{
static $count = 0;
$count + +;
Echo $count;
if ($count < 10) {
Test ();
}
$count--;
}
?>

The results of the implementation are as follows:
This is a recursive function that declares the number of static variables count records, output 1~10.
I look at the time there is a doubt, recursive call when static $count = 0; Statement repeats, why does it not cause the count variable to be repeatedly assigned? With this question and colleagues to study, the test code is as follows:
Copy Code code as follows:

<?php
echo ' start<br/> ';
static $a = 10;
echo "$a <br/>";
unset ($GLOBALS [' a ']);
echo "$a <br/>";
static $a = 20;
echo "$a <br/>";
$GLOBALS [' a '] = 10;
echo "$a <br/>";
static $a = 30;
echo "$a <br/>";
unset ($GLOBALS [' a ']);
echo "$a <br/>";
static $a;
echo "$a <br/>";
static $a = 40;
echo "$a <br/>";
$a = 100;
echo "$a <br/>";
static $a = 50;
echo "$a <br/>";
static $a = 4;
echo "$a <br/>";
echo ' End <br/> ';
Exit
?>

The results of the implementation are as follows:
Start
  1. 4
  2. Notice: Undefined variable:a
  3. 4
  4. 10
  5. 10
  6. Notice: Undefined variable:a
  7. 10
  8. 10
  9. 100
  10. 100
  11. 100
  12. End
(the part of the result regarding the location of the document has been deleted.) You can also remove the Echo statement using the Zend Debug feature to see the results more clearly.

The first output $a of code line 5th is 4, thus inferring that PHP allocates the memory of the static variable when the page is initialized, using the value of the last declaration of the same variable (this can change 4 to a different number of tests). Line 7th of the code calls the Unset function to destroy the variable $a, again outputting the $a value to see a hint of undefined variable, indicating that the variable has been destroyed.

Line 10th again output, the output is still 4 instead of 20, there are two possibilities, one is the PHP initialization of the $a value, the other is PHP use the $a is destroyed before the value, the problem in the 20th row output time to resolve. The 16th line $a destroys when the value is 10, and the 19th line declares that the output is still 10.

The 11th line of the $a is modified to 10, and the $a,17 row output is again declared as 10 on 14 lines. It is assumed that PHP uses the value in the static variable in memory instead of assigning it again when repeating the declaration.

At this point, the problem found in the manual has been largely resolved, that is, the declaration in the recursive call did not change the value of the $count, so recursion stopped successfully at $count=10.

may have misunderstood the place, welcome to Pat Bricks.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.