daily pastebin goal
72%
SHARE
TWEET

PHP STATIC VARIABLE EXAMPLE

golammostofa Sep 25th, 2017 (edited) 51 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. http://www.webdeveloper.com/forum/showthread.php?67283-how-to-declare-static-variable-in-php
  8.  
  9. *******/
  10.  
  11. //static vs non static variables
  12. function incrementStatic()
  13. {
  14.   static $myVar = 0;
  15.   $myVar++;
  16.   return($myVar);
  17. }
  18. echo "Static : <pre>";
  19. echo incrementStatic() . "\n";
  20. echo incrementStatic() . "\n";
  21. echo incrementStatic() . "\n";
  22.  
  23.  
  24. function incrementNormal()
  25. {
  26.   $myVar = 0;
  27.   $myVar++;
  28.   return($myVar);
  29. }
  30. echo "\nNormal <pre>";
  31. echo incrementNormal() . "\n";
  32. echo incrementNormal() . "\n";
  33. echo incrementNormal() . "\n";
  34.  
  35. ?>
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top