Foreach Loop In PHP: Definition, Functions & Uses

The foreach loop in PHP construct produces the most comfortable way to iterate the array components. It runs on arrays and objects both. The foreach loop iterates over an array of entities; the enactment is streamlined and completes the loop in a limited time comparatively. It distributes temporary memory for index iterations, making the comprehensive system duplicative in terms of memory allocation.

foreach( $array as $element ) {

    // PHP Code to be executed


Read: PHP Developer Salary in India

Definition, Functions, and Uses of Foreach Loop

This question interests how it functions behind the scene, and we don’t require any answers along the lines of “this is how we loop an array with PHP foreach loop.”

For a long time, we inferred that foreach operated with the array itself. Then we found numerous references to the information that it functions with a copy of the array and have since assumed this to be the end of the statement. But recently got into a conversation on the course, and after a little investigation, found that this was not 100% valid.

Let us show what we imply. For the following test cases, we will be helping with the following array:

$array = array(1, 2, 3, 4, 5, 6);

foreach ($array as $item) { 

echo “$item\n”; $array[] = $item; }


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

This certainly shows that we are not working instantly with the source array – on the contrary, the loop would proceed forever, since we are always pushing elements onto the array during the loop. But just to be confident this is the case:

foreach ($array as $key => $item) {

  $array[$key + 1] = $item + 2;

  echo “$item\n”;



/* Output in loop: 2 3 4 5

   $array after loop: 3 4 5 6 7 8 */

This supports our preliminary conclusion; we are toiling with a copy of the source array during the loop. Contrarily, we would recognize the modified values during the loop.

If we look in the guide, we learn this statement:

When foreach first starts committing, the inner array pointer is automatically reset to the array’s initial element.

Straight- this seems to imply that foreach is sure of the array pointer of the source array. But we’ve just substantiated that we’re not labouring with the source array. Well, not completely.

// 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) */

Iteration of arrays and common objects is considerably more difficult. First of all, it should be cited that in PHP, “arrays” are organized dictionaries, and they will be spanned according to this rule (which approximates the insertion order as long as you didn’t utilize something like sort). This is contradicted to iterating by the normal order of the keys (how lists in different languages always work) or having no distinguished order at all (how dictionaries in different languages continually work).

The similarity also pertains to objects, as the object properties can be discerned as another one (ordered) dictionary mapping property terms to their values, plus some sight handling. In the preponderance of cases, the object properties are not bottled in this rather inadequate way.

Still, if you start iterating over an object, the full representation commonly used will be restored to a real dictionary. At that point, the iteration of ordinary objects becomes very identical to an iteration of arrays.

Iterating over a dictionary can’t be too difficult. The difficulties start when we understand that an array/object can shift during iteration. The difficulty with permitting improvements during iteration is the possibility where the element we are presently on is eliminated. Say we use a pointer to keep a trail of which array element we are presently at. If this element is now exempted, we are left with a dangling pointer.

Also Read: 15 Interesting PHP Projects on Github For Beginners


PHP foreach loop is utilized for looping through the values of an array. It loops over the array, and each value for the fresh array element is assigned to value, and the array pointer is progressed by one to go the following element in the array.

If you’re interested to learn more about PHP, Full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Land on Your Dream Job

Enroll Today

Leave a comment

Your email address will not be published.

Let’s do it!
No, thanks.