/ Published in: PHP
References are like aliases. Instead of assigning memory to the new variable, you're simply pointing to the previous variable.
Expand |
Embed | Plain Text
/************************ Example 1 ************************/ $a = 1; $b = $a; $b = 2; //$b is now going to become a reference to $a or in other words, an alias $a = 1; $b =& $a; $b = 2; //Unset the $b reference (or alias) //$b will not be set to nothing echo "a:{$a} / b: {$b}"; echo "<hr />"; /************************ Example 2: Using References as Function Arguments ************************/ //Version 1 function example1(){ global $a; $a = $a + 1; } $a = 10; example1( $a ); //Version 2 function example2( &$var ){ //When something comes in, don't take its value, make a reference of it's value $var = $var + 1; } $a = 10; example2( $a );
You need to login to post a comment.
