Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Test this online on http://phptester.net/
- echo '<pre>';
- echo "1. Passing variable by value.\n";
- $var1 = 'foo'; // define it
- $func = function() use ( $var1 ){
- echo "$var1\n";
- };
- $func(); // outputs 'foo'
- $var1 = 'bar'; // change the value
- $func(); // still outputs 'foo'
- echo "\n2. Passing variable by reference.\n";
- $var2 = 'abc'; // define it
- $func = function() use ( &$var2 ){
- echo "$var2\n";
- $var2 = 'def'; // change the value
- };
- $func(); // outputs 'abc'
- echo "$var2\n"; // outputs 'def'
- $var2 = '123'; // change the value
- $func(); // outputs '123'
- echo "$var2\n"; // outputs 'def'
- echo '</pre>';
Add Comment
Please, Sign In to add comment