5

I'm studying PHP for an advanced exam. The practice test said that the first iteration is better than the second. I not figure out why. They both iterate the contents of an array just fine.

// First one:

 foreach($array as $key => &$val) { /* ... */ }

// Second one:

foreach($array as $key => $val) { /* ... */ }
Willow
  • 1,040
  • 2
  • 10
  • 21
  • Yeah it tends to confuse people better: http://stackoverflow.com/questions/6287970/php-foreach-statement-by-reference-unexpected-behaviour-when-reusing-iterator – Andreas Wong May 15 '13 at 06:51
  • I suggest you to read [this](http://stackoverflow.com/questions/10057671/how-foreach-actually-works). – BlitZ May 15 '13 at 06:51
  • 3
    What exam? I can't believe this was from ZCE – Mark Baker May 15 '13 at 06:53
  • Related: [Fastest way to iterate array in PHP](http://stackoverflow.com/questions/1596988/fastest-way-to-iterate-array-in-php) – Theraot Feb 16 '14 at 09:50

2 Answers2

10

The practice test said that the first iteration is better than the second.

That isn't the best advice. They're different tools for different jobs.

The & means to treat the variable by reference as opposed to a copy.

When you have a variable reference, it is similar to a pointer in C. Accessing the variable lets you access the memory location of the original variable, allowing you to modify its value through a different identifier.

// Some variable.
$a = 42;

// A reference to $a.
// & operator returns a reference.
$ref = &$a;

// Assignment of $ref.
$ref = "fourty-two";

// $a itself has changed, through
// the reference $ref.
var_dump($a); // "fourty-two"

Reference example on CodePad.

The normal behaviour of foreach is to make a copy available to the associated block. This means you are free to reassign it, and it won't affect the array member (this won't be the case for variables which are always references, such as class instances).

Copy example on CodePad.

Class instance example on CodePad.

Using a reference in a foreach has some side effects, such as a dangling $val reference to the last value iterated over, which can be modified later by accident and affect the array.

Dangling reference example on CodePad.

alex
  • 479,566
  • 201
  • 878
  • 984
  • Thanks for the help! Could you say a bit more about "reference as opposed to a copy"? My only understanding of & would be writing over a variable such as $Foo2 = &$Foo1; – Willow May 15 '13 at 07:03
  • I see. So if I use & I can edit the array outside the foreach loop by calling $val? Thanks VERY much! You did an amazing job at making this clear. – Willow May 15 '13 at 07:25
  • I still need a lot more practice/learning before I can take the PHP UNIT Test. Do you have any suggestions on good websites to help me study? – Willow May 15 '13 at 07:26
  • 1
    @Willow That issue generally leads to more problems than usefulness. As for getting to know PHP, read a lot of Stack Overflow questions and ask when you get stuck, after you've given it the best you can. – alex May 15 '13 at 09:19
1

The first example includes the & reference-operator for $val --> any changes to $val will be saved to $array.

This is by no means "better" than example #2, though.

michi
  • 6,565
  • 4
  • 33
  • 56