64

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I need to know why we use ampersand before the variable in foreach loop

foreach ($wishdets as $wishes => &$wishesarray) {
    foreach ($wishesarray as $categories => &$categoriesarray) {

    }
}
Anders
  • 8,307
  • 9
  • 56
  • 88
Sachin Sawant
  • 801
  • 2
  • 8
  • 6
  • 4
    Important gotcha with this is that the loop variable is leaked outside the loop scope (because PHP doesn't have block scope, to make things more fun). So if you later assign to the loop variable, you corrupt the array. This can happen even if you use it as a loop variable in another loop, leading to very "fun" errors. A solution is to unset() the loop variable after the loop. – MightyPork Oct 03 '18 at 10:17
  • $array = array(1, 2); foreach ($array as $value) { $value++; } print_r($array); // 1, 2 because we iterated over copy of value This means it is passed by reference instead of array value, it variable will affect the original array value. $array = array(1, 2, 3, 4,5); echo "
    ";
    print_r($array);
    
    foreach ($array as &$value) 
    {
        $value = $value*2;
    }
    echo "
    ";
    print_r($array); // 2, 4,6,8,10 because we iterated over references to actual values of array
    – Vikas kumar May 13 '22 at 04:25

4 Answers4

81

This example will show you the difference

$array = array(1, 2);
foreach ($array as $value) {
    $value++;
}
print_r($array); // 1, 2 because we iterated over copy of value

foreach ($array as &$value) {
    $value++;
}
print_r($array); // 2, 3 because we iterated over references to actual values of array

Check out the PHP docs for this here: http://pl.php.net/manual/en/control-structures.foreach.php

Dave Heq
  • 356
  • 4
  • 18
Mariusz Sakowski
  • 3,232
  • 14
  • 21
28

This means it is passed by reference instead of value... IE any manipulation of the variable will affect the original. This differs to value where any modifications don't affect the original object.

This is asked many times on stackoverflow.

AlanFoster
  • 8,156
  • 5
  • 35
  • 52
  • 4
    To add, "By reference" means it's referring to the same allocated memory block for the variable being referred to – Prof Jan 07 '12 at 11:27
  • 2
    as opposed to "by value", which means a copy of the variable is created by PHP in a new memory block. – Kaii Jan 07 '12 at 11:34
  • There are two ways of how the reference is passed in the questions example code, [I outlined both](http://stackoverflow.com/a/8769347/367456). – hakre Jan 07 '12 at 11:37
  • 9
    If you know "this is asked many times on StackOverflow" then why do you answer it? Find a suitable duplicate it and provide as a comment (or closevote once you reached 3k) – Gordon Jan 07 '12 at 12:15
9

It is used to apply changes in single instance of array to main array..

As:

//Now the changes wont affect array $wishesarray

foreach ($wishesarray as $id => $categoriy) {
      $categoriy++;
}
print_r($wishesarray); //It'll same as before..

But Now changes will reflect in array $wishesarray also

foreach ($wishesarray as $id => &$categoriy) {
      $categoriy++;
}
print_r($wishesarray); //It'll have values all increased by one..
Rajat Singhal
  • 11,234
  • 5
  • 38
  • 56
3

For the code in your question, there can be no specific answer given because the inner foreach loop is empty.

What I see with your code is, that the inner foreach iterates over a reference instead of the common way.

I suggest you take a read of the foreach PHP Manual page, it covers all four cases:

foreach($standard as $each);

foreach($standard as &$each); # this is used in your question

$reference = &$standard;
foreach($reference as $each);

$reference = &$standard;
foreach($reference as &$each); # this is used in your question
hakre
  • 193,403
  • 52
  • 435
  • 836