How does PHP foreach actually work

0 votes

Let me start by stating that I am familiar with foreach's functions and usage. I don't want any replies that say, "This is how you loop an array with foreach," because this question is about how it actually operates.

I believed for a very long time that foreach just applied to the array itself. After discovering numerous references to the fact that it functions with a copy of the array, I came to the conclusion that this was the end of the matter. However, I recently engaged in a conversation on the subject and discovered through some experiments that this was not quite accurate.

Test case 1:

foreach ($array as $item) {
  echo "$item\n";
  $array[] = $item;
}
print_r($array);

/* Output in loop:    1 2 3 4 5
   $array after loop: 1 2 3 4 5 1 2 3 4 5 */

Since we are continuously adding items to the array while the loop is running, this clearly demonstrates that we are not interacting directly with the source array. But to be certain that this is the case:

Test case 2:

foreach ($array as $key => $item) {
  $array[$key + 1] = $item + 2;
  echo "$item\n";
}

print_r($array);

/* Output in loop:    1 2 3 4 5
   $array after loop: 1 3 4 5 6 7 */

This backs up our initial conclusion, we are working with a copy of the source array during the loop, otherwise we would see the modified values during the loop. But...

If we look in the manual, we find this statement:

When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array.

Test case 3:

// Move the array pointer on one to make sure it doesn't affect the loop
var_dump(each($array));

foreach ($array as $item) {
  echo "$item\n";
}

var_dump(each($array));

/* Output
  array(4) {
    [1]=>
    int(1)
    ["value"]=>
    int(1)
    [0]=>
    int(0)
    ["key"]=>
    int(0)
  }
  1
  2
  3
  4
  5
  bool(false)
*/

The fact that the source array pointer is at the end of the array at the end of the loop indicates that, while not working directly with the source array, we are working directly with it. However, if this were the case, test case 1 would loop indefinitely.

Test case 4:

foreach ($array as $key => $item) {
  echo "$item\n";
  each($array);
}

/* Output: 1 2 3 4 5 */

Test case 5:

foreach ($array as $key => $item) {
  echo "$item\n";
  reset($array);
}

/* Output: 1 2 3 4 5 */

Question

What is going on here? My C-fu is not good enough for me to able to extract a proper conclusion simply by looking at the PHP source code, I would appreciate it if someone could translate it into English for me.

It seems to me that foreach works with a copy of the array, but sets the array pointer of the source array to the end of the array after the loop.

  • Is this correct and the whole story?
  • If not, what is it really doing?
  • Is there any situation where using functions that adjust the array pointer (each(), reset() et al.) during a foreach could affect the outcome of the loop?

Aug 7, 2022 in PHP by Kithuzzz
• 38,000 points
1,004 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
0 answers

How does true/false work in PHP?

I'm curious about PHP's internal handling of ...READ MORE

Aug 4, 2022 in PHP by Kithuzzz
• 38,000 points
479 views
0 votes
1 answer

How foreach change original array values in php?

Hello @kartik, Do the following: foreach ($fields as $key ...READ MORE

answered Aug 14, 2020 in PHP by Niroj
• 82,840 points
8,776 views
0 votes
0 answers

What Does 6k views mean and how can I format the number in PHP?

What does "6k views" mean and how ...READ MORE

Jun 2, 2022 in PHP by Kichu
• 19,040 points
523 views
0 votes
0 answers

PHP FILTER_VALIDATE_EMAIL does not work correctly

I employ PHP 5.3.10. Here is the ...READ MORE

Aug 4, 2022 in PHP by Kithuzzz
• 38,000 points
906 views
0 votes
1 answer

How can we avoid my php form from hacking?

Hii @kartik, If you want to know php ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
2,607 views
0 votes
1 answer

How to Validate Form Data With PHP?

Hey @kartik, The first thing we will do ...READ MORE

answered Feb 13, 2020 in PHP by Niroj
• 82,840 points
3,373 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8, 2022 in Java by Rahul
• 9,680 points
382 views
0 votes
0 answers

For-each over an array in JavaScript

How can I use JavaScript to loop ...READ MORE

Sep 21, 2022 in Java by Nicholas
• 7,760 points
560 views
0 votes
0 answers

Loop over an array in JavaScript

How can I use JavaScript to loop ...READ MORE

Dec 9, 2022 in Java by Nicholas
• 7,760 points
655 views
0 votes
1 answer

foreach equivalent in Python

You can use a for loop as ...READ MORE

answered Oct 4, 2018 in Python by SDeb
• 13,300 points
1,546 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP