Using references, you can access the same value with different names. Let me explain this with an example.
Let’s create a variable that holds integer value.
$a = 10;
Let’s assign the reference of variable $a to $b.
$b = & $a;
As shown in the above image, both the variable $a, $b are pointing to same memory location.
Now if you update the variable $b, same will reflect to $a and vice versa.
$b = 1111;
variable_reference_demo.php
#!/usr/bin/php
<?php
$a = 10;
$b = & $a;
echo "\$a : $a\n";
echo "\$b : $b\n";
echo "\nSetting \$b value to 1111\n\n";
$b = 1111;
echo "\$a : $a\n";
echo "\$b : $b\n";
?>
Output
$./variable_reference_demo.php
$a : 10
$b : 10
Setting $b value to 1111
$a : 1111
$b : 1111
Previous Next Home
No comments:
Post a Comment