References

Using the equal sign (=) copies the value from one variable to another so they both have their own copy of the value. Another option here is to use references, which is where a variable does not have a value of its own; instead, it points to another variable. This enables you to share values and have variables mutually update themselves.

To copy by reference, use the & symbol, as follows:

$a = 10; $b = &$a; echo $a . "\n"; echo $b . "\n";

If you run that script, you will see that updating $a also updates $b, but also that updating $b updates $a, too.

Was this article helpful?

0 0

Post a comment