fork download
  1. <?php
  2.  
  3. /******
  4. - We use constants like PI, do not expect to ever need to change during the program's run.
  5. - Static variables are, in fact, variable. The "static" means that the variable retains its value between calls to whatever that variable's scope is (function, class, procedure, etc.). PHP only supports static variables within the scope of a function definition.
  6.  
  7. *******/
  8.  
  9. //static vs non static variables
  10. function incrementStatic()
  11. {
  12. static $myVar = 0;
  13. $myVar++;
  14. return($myVar);
  15. }
  16. echo "Static : ";
  17. echo incrementStatic() . "\n";
  18. echo incrementStatic() . "\n";
  19. echo incrementStatic() . "\n";
  20.  
  21.  
  22. function incrementNormal()
  23. {
  24. $myVar = 0;
  25. $myVar++;
  26. return($myVar);
  27. }
  28. echo "\nNormal: ";
  29. echo incrementNormal() . "\n";
  30. echo incrementNormal() . "\n";
  31. echo incrementNormal() . "\n";
  32.  
  33. ?>
  34.  
Success #stdin #stdout 0s 82880KB
stdin
Standard input is empty
stdout
Static : 1
2
3

Normal: 1
1
1