This question already has an answer here:
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference from http://php.net/manual/en/control-structures.foreach.php.
$arr = array(1, 2, 3, 4);
foreach ($arr as &$value) {
echo $value;
}
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value;
}
In both cases, it outputs 1234. What does adding & to $value actually do? Any help is appreciated. Thanks!
</div>
In the beginning when learning what passing by reference it isn't obvious....
Here's an example that I hope will hope you get a clearer understanding on what the difference on passing by value and passing by reference is...
<?php
$money = array(1, 2, 3, 4); //Example array
moneyMaker($money); //Execute function MoneyMaker and pass $money-array as REFERENCE
//Array $money is now 2,3,4,5 (BECAUSE $money is passed by reference).
eatMyMoney($money); //Execute function eatMyMoney and pass $money-array as a VALUE
//Array $money is NOT AFFECTED (BECAUSE $money is just SENT to the function eatMyMoeny and nothing is returned).
//So array $money is still 2,3,4,5
echo print_r($money,true); //Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )
//$item passed by VALUE
foreach($money as $item) {
$item = 4; //would just set the value 4 to the VARIABLE $item
}
echo print_r($money,true); //Array ( [0] => 2 [1] => 3 [2] => 4 [3] => 5 )
//$item passed by REFERENCE
foreach($money as &$item) {
$item = 4; //Would give $item (current element in array)value 4 (because item is passed by reference in the foreach-loop)
}
echo print_r($money,true); //Array ( [0] => 4 [1] => 4 [2] => 4 [3] => 4 )
function moneyMaker(&$money) {
//$money-array is passed to this function as a reference.
//Any changes to $money-array is affected even outside of this function
foreach ($money as $key=>$item) {
$money[$key]++; //Add each array element in money array with 1
}
}
function eatMyMoney($money) { //NOT passed by reference. ONLY the values of the array is SENT to this function
foreach ($money as $key=>$item) {
$money[$key]--; //Delete 1 from each element in array $money
}
//The $money-array INSIDE of this function returns 1,2,3,4
//Function isn't returing ANYTHING
}
?>
It denotes that you pass $value by reference. If you change $value within the foreach loop, your array will be modified accordingly.
Without it, it'll passed by value, and whatever modification you do to $value will only apply within the foreach loop.
This is reference to a variable and the main use in foreach loop is that you can change the $value variable and that way the array itself would also change.
when you're just referencing values, you won't notice much difference in the case you've posted. the most simple example I can come up with describing the difference of referencing a variable by value versus by reference is this:
$a = 1;
$b = &$a;
$b++;
print $a; // 2
You'll notice $a is now 2 - because $b is a pointer to $a. if you didn't prefix the ampersand, $a would still be 1:
$a = 1;
$b = $a;
$b++;
print $a; // 1
HTH
Normally every function creates a copy of its parameters, works with them, and if you don't return them "deletes them" (when this happens depends on the language).
If run a function with &VARIABLE
as parameter that means you added that variable by reference and in fact this function will be able to change that variable even without returning it.