Advertisement
golammostofa

PHP STATIC VARIABLE EXAMPLE

Sep 25th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.93 KB | None | 0 0
  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. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement