5ally

Untitled

Mar 19th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.64 KB | None | 0 0
  1. <?php
  2. // Test this online on http://phptester.net/
  3.  
  4. echo '<pre>';
  5.  
  6. echo "1. Passing variable by value.\n";
  7.  
  8. $var1 = 'foo'; // define it
  9. $func = function() use ( $var1 ){
  10.     echo "$var1\n";
  11. };
  12.  
  13. $func(); // outputs 'foo'
  14. $var1 = 'bar'; // change the value
  15. $func(); // still outputs 'foo'
  16.  
  17.  
  18. echo "\n2. Passing variable by reference.\n";
  19.  
  20. $var2 = 'abc'; // define it
  21. $func = function() use ( &$var2 ){
  22.     echo "$var2\n";
  23.     $var2 = 'def'; // change the value
  24. };
  25.  
  26. $func(); // outputs 'abc'
  27. echo "$var2\n"; // outputs 'def'
  28. $var2 = '123'; // change the value
  29. $func(); // outputs '123'
  30. echo "$var2\n"; // outputs 'def'
  31.  
  32. echo '</pre>';
Add Comment
Please, Sign In to add comment