PHP References

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Syntax

  • $foo = 1; $bar = &$foo; // both $foo and $bar point to the same value: 1
  • $var = 1; function calc(&$var) { $var *= 15; } calc($var); echo $var;

Remarks

While assigning two variables by reference, both variables point to the same value. Take the following example:

$foo = 1;
$bar = &$foo;

$foo does not point to $bar. $foo and $bar both point to the same value of $foo, which is 1. To illustrate:

$baz = &$bar;
unset($bar);
$baz++;

If we had a points to relationship, this would be broken now after the unset(); instead, $foo and $baz still point to the same value, which is 2.



Got any PHP Question?