Home  >  Article  >  Backend Development  >  Detailed explanation of php static variables_php static variables and global static variables examples

Detailed explanation of php static variables_php static variables and global static variables examples

WBOY
WBOYOriginal
2016-07-25 08:52:39980browse
  1. function test()
  2. {
  3. static $var = 5; //static $var = 1+1; will report an error
  4. $var++;
  5. echo $var . ' ';
  6. }
  7. test() ; //2
  8. test(); //3
  9. test(); //4
  10. echo $var; //Error: Notice: Undefined variable: var
Copy code

Part 2, php static variables Global type, let’s understand it through examples.

  1. //Global variables themselves are static storage methods, and all global variables are static variables

  2. function static_global(){
  3. global $glo;
  4. $glo++;
  5. echo $glo.'
    ';
  6. }

  7. static_global(); //1

  8. static_global(); //2
  9. static_global(); //3
  10. echo $glo . '
Copy code

Tips: Static global variables are not used much.



Statement:
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