PHP: Moderately Complicated Reference Example


/ Published in: PHP
Save to your folder(s)

This shows you how to return a reference from a method.


Copy this code and paste it in your HTML
  1. //This function is being returned as a reference
  2. function &example1(){
  3. global $a;
  4. $a = $a + 5;
  5. return $a;
  6. }
  7.  
  8. $a = 10;
  9.  
  10. $b =& example1();
  11.  
  12. echo "a:{$a} / b: {$b}" . "<br />";
  13.  
  14. $b = 30;
  15.  
  16. echo "a:{$a} / b: {$b}" . "<br />";
  17.  
  18. function &increment(){
  19. static $var = 0;
  20. $var++;
  21. return $var;
  22. }
  23.  
  24. $a =& increment();
  25. increment();
  26. $a++;
  27. increment();
  28. echo "a:{$a}" . "<br />";

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.