-4

Could you explain, in short (because reading and understanding How does PHP 'foreach' actually work? takes a lot of time), why does this infinite loop:

$foo = array(0, 1); foreach($foo as &$v) $foo[] = 42;

whereas this does not:

$foo = array(0); // 1 element at initialization foreach($foo as &$v) $foo[] = 42;

nor does this

$foo = array(0, 1); foreach($foo as $v) // not using a ref as foreach iterator $foo[] = 42;

?

Community
  • 1
  • 1
Xenos
  • 3,351
  • 2
  • 27
  • 50
  • 5
    I'm so sorry that learning is too time consuming for you. I'm still awaiting *import to brain* functionality myself. In the meantime, may I suggest that you pay someone who actually wants to learn to do your programming for you? – eggyal Apr 23 '14 at 17:11
  • 2
    I stopped reading at "*because reading and understanding "How 'foreach' actually works" takes a lot of time*". -_- – Amal Murali Apr 23 '14 at 17:11
  • Somebody upvoted because OP doesn't want to read a post that they cited that explains exactly what they are asking? – AbraCadaver Apr 23 '14 at 17:11
  • Please read documentation you posted. – Goku Nymbus Apr 23 '14 at 17:12
  • 2
    Here's a link you can read to understand [how a foreach loop works](http://stackoverflow.com/questions/10057671/how-foreach-actually-works). – Kryten Apr 23 '14 at 17:13
  • @eggyal: yeah, that was my snide way of telling him to take the time to read & understand the resource he already has. – Kryten Apr 23 '14 at 17:19
  • You should RTFM. Anyway, the short version of the answer is in my answer. The long version is in the document, so go AND read it COMPLETELY. – Luis Masuelli Apr 23 '14 at 17:22
  • Year, people should learn chemistry before cooking, they should read the whole dictionary instead of asking for a word, and google is useless because one should just take the time to crawl the whole Internet. I was looking for a specific point (maybe wrongly worded), so yes, thanks Luis. – Xenos Apr 23 '14 at 17:59

2 Answers2

1

Your first loop, the infinite one, is updating the array WITHIN the loop. Every time you push a new element onto the end of the array with [], you're EXTENDING the array with a new element for the foreach to iterate over.

what's happening is:

  1. foreach initializes
  2. there's two elements, so process the first one
  3. since there's more elements left, the internal "continue the foreach" flag is set
  4. you push a new element onto the array (now there's 3)
  5. the foreach gets a new element off the array, and... there's STILL an element left (Your new #3), so the "continue the loop" flag stays set
  6. you push element #4 on, and so on.

With the other arrays, you're starting out with a SINGLE element

  1. foreach initializes
  2. there's one element, so process it
  3. there's no more elements after this first one, so "end the loop" flag gets set
  4. you push your new element
  5. the loop terminates, because the test for continuation was performed BEFORE you pushed the new element.

Essentially, you've strapped a carrot to a stick in front of PHP and it's just following behind the carrot.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • In the third case, the situation is that the loop is performed over a copy of the array so pushing a new element onto it does not affect the copy over which the loop is performed. – eggyal Apr 23 '14 at 17:15
1

The first array is iterated BY REFERENCE, as the second array. This is because iterated elements are referenced (&$v) and so the original array MUST be affected. If you extend the array variable, the same iterated array is extended infinitely.

For the second array is the same case, but the condition test to perform the next iteration was done beforehand, so it doesn't matter the array is extended by one, it's marked as terminated in that test.

The third array is iterated by value (and thus copied). PHP does that if no reference is needed to the array (or array values in the foreach loop; or the array itself is not a reference regarding it's zval). So extending the array will not alter the iterated array (since it's a copy and not the array in the variable).

More: (guess where?) Here

Community
  • 1
  • 1
Luis Masuelli
  • 12,079
  • 10
  • 49
  • 87