<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
?>
In the above example, after the code is executed, $value will remain in scope and will hold a reference to the last element in the array.
<?php
$array = [1, 2, 3];
echo implode(',', $array), "\n";
foreach ($array as &$value) {}
// by reference
echo implode(',', $array), "\n";
foreach ($array as $value) {}
// by value (i.e., copy)
echo implode(',', $array), "\n";
?>
The above code will output the following: 1,2,3 1,2,3 1,2,2
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
$value = $value * 2;
}
unset($value);
// $value no longer references $arr[3]
https://www.toptal.com/php/10-most-common-mistakes-php-programmers-make