Sample code for PHP static variable static demo _php tutorial

Source: Internet
Author: User
Tags parse error
InThis function is of little use because the value of $w 3sky is set to 0 and output "0" each time it is called. Adding a variable to a $w 3sky++ has no effect, because once you exit this function, the variable $w 3sky does not exist. To write a count function that does not lose this count value, define the variable $w 3sky as static:

Example using PHP static variable statics

 
  
  
  1. < ? PHP
  2. function Test () {
  3. Static $ W3sky 0;
  4. echo $w 3sky;
  5. $w 3sky++;
  6. }
  7. ?>

Now, each call to the Test () function outputs the value of $w 3sky and adds one.

Static variables also provide a way to handle recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because they can be recursive indefinitely. You must ensure that there is sufficient method to abort the recursion. This simple function recursively counts to 10, using a static variable $count to determine when to stop:

Example PHP static variable statics and recursive functions

 
  
  
  1. < ? PHP
  2. function Test () {
  3. Static $ Count 0;
  4. $count + +;
  5. Echo $count;
  6. <) {
  7. Test ();
  8. }
  9. $count--;
  10. }
  11. ?>

Note: Static variables can be declared according to the example above. If you assign a value to a declaration with the result of an expression, it causes a parse error.

Example declaration PHP static variable static

 
  
  
  1. < ? PHP
  2. function foo () {
  3. Static $ int 0;// correct
  4. Static $ int 1+2;//Wrong (as it is an expression_r_r)
  5. Static $ int sqrt(121);//Wrong (as it is an expression_r_r too)
  6. $int + +;
  7. Echo $int;
  8. }
  9. ?>


http://www.bkjia.com/PHPjc/446285.html www.bkjia.com true http://www.bkjia.com/PHPjc/446285.html techarticle This function is of little use, since the value of $w 3sky is set to 0 and output "0" each time it is called. Adding a variable to a $w 3sky++ has no effect, because once exiting this function the variable ...

  • 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.