PHP foreach by value vs foreach by reference

When you use a foreach loop, PHP creates a copy of the current element you are using in your loop. Any changes you make to this element is not saved to the original array. See the first part of the following example:

<?php
$a = array('a','b','c');
foreach ($a as $b) {
 $b = 'z';
}
print_r($a);
foreach ($a as &$b) {
 $b = 'z';
}
print_r($a);
?>

In the second part of the example, we access the array elements by reference using &$b. This allows us to point directly to the variable rather than its copy. Any changes to this variable is a change made to the array element. The output of the program is as follows:

Array
(
 [0] => a
 [1] => b
 [2] => c
)
Array
(
 [0] => z
 [1] => z
 [2] => z
)
Technologies: 
Actions: