References¶
References allow creating multiple variables that refer to the same underlying value in memory. When using references, any changes made by one variable affect the others that reference the same value. This may be useful in certain situations, such as when it is important to avoid copying large amounts of data or when it is needed to modify the original value through multiple variables. PHP optimizes the transfert of data by postponing duplication of data until they are actually modified: there is no need to use references with readonly data.
<?php
$a = 1;
$b = &$a;
$b = 2;
echo $a;
// displays 2
?>
See also https://www.elated.com/php-references/, https://www.tutorialspoint.com/php-spotting-references, https://erikpoehler.com/2023/01/15/references-in-php/
Related : Variables, Weak References, Alias, Dangling Reference, Garbage Collection, Recursive Array, Self-reference