PHP static variable example code demo _ PHP Tutorial

Source: Internet
Author: User
PHP static variable static sample code demonstration. This function is useless because $ w3sky is set to 0 and 0 is output every time you call the function. Adding $ w3sky ++ to the variable does not work, because once you exit this function, the variableThis function is useless because $ w3sky is set to 0 and "0" is output every time you call it ". Adding $ w3sky ++ to the variable does not work because $ w3sky does not exist once you exit this function. To write a counting function that does not lose the current count value, you must define the variable $ w3sky as static:

Example of static PHP variables

 
 
  1. < ?PHP
  2. function Test(){
  3. static $w3sky = 0;
  4. echo $w3sky;
  5. $w3sky++;
  6. }
  7. ?>

Now, every time you call the Test () function, the value of $ w3sky is output and added with one.

Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion. The simple function calculates 10 recursively and uses the static variable $ count to determine when to stop:

Example of static and recursive functions in PHP

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

Note: static variables can be declared according to the preceding example. If a value is assigned to the expression result in the declaration, the parsing error occurs.

Example declaration PHP static variable

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


This function is useless because $ w3sky is set to 0 and "0" is output every time you call the function ". Adding $ w3sky ++ to the variable does not work, because once you exit 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.