• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between For and Foreach in PHP

In PHP there are four types of loops in general, while, do while, for and foreach. In this article, we are going to examine the differences between for and foreach, where the prior difference between them is that for loop uses iterator variable while foreach works only on interator array.

Looping in a programming language play a crucial role in any programming construct. The primary function of a loop is to execute the same block of code several times, repeatedly until a certain condition is satisfied.

Content: For Vs Foreach

    1. Comparison Chart
    2. Definition
    3. Key Differences
    4. Conclusion

Comparison Chart

Basis for comparisonForForeach
Implemented overVariable/sNumerical and associative arrays
WorkingAt the end of the given conditionAt the end of the array count
Types of implementation
Single
Two
Syntax
for(expr1; expr2; expr3)
{//If expr2 is true, do this}
foreach ($array as $value)
{//Do Something}
//Another form, for key & values
foreach ($array as $key => $value)
{//Do Something}

Definition of For

The for loop is a more concise form of the other loops like while and do while in PHP. In the while loop, a counter is set to start with, then it is tested in a condition before each iteration. At last, the counter is modified at the end of each iteration. While in for loop, the expressions and condition are defined at a time inside the for loop parenthesis as shown in the structure below:

for ( initialization; Condition; Increment)
{
Code to be executed or Statements;
}

The for loops executes a code block again and again until the condition is falsified. The elements of a for loop are given below:

  • Initialization: The initial values are assigned to the counter variables which is evaluated once unconditionally at the beginning and before the first execution of the body of the loop.
  • Condition: The condition expression is evaluated prior to each iteration. If the condition is true the nested statements are executed otherwise the execution will be ceased.
  • Increment: This expression modifies the value of the loop counter and is evaluated in the end of each iteration.
  • Statements: The code and statements are executed one time in each iteration.

Example of for loop

<?php
for ($i = 1 ; $i <= 10 ; ++$i)
echo "$i minutes has " . $i * 60 . " seconds"."<br>";
?>

Output:

1 minutes has 60 seconds
2 minutes has 120 seconds
3 minutes has 180 seconds
4 minutes has 240 seconds
5 minutes has 300 seconds
6 minutes has 360 seconds
7 minutes has 420 seconds
8 minutes has 480 seconds
9 minutes has 540 seconds
10 minutes has 600 seconds

Definition of Foreach

The foreach loop is quite different from the for a loop as it permits the iteration of the elements in an array. To make the language more convenient for the programmers, the developers of PHP provided a loop structure specifically designed for the arrays, since the basic loop is not capable of performing operations with an array. Unlike for loop, the foreach does not require any initialization and termination expressions. The foreach loop work in a proper way with list() and each() constructs also.

The array elements can be tracked easily by the predefined indexing, but this requires a large amount of memory space to remember the exact numbering of the item. So, associative arrays can be used in place of a numerically indexed array which reference the items in an array by name instead of a number.

Working of a foreach loop

The process begins with the first item and terminates with the last one, so the programmer need not know the number of items existing in the array.

At the time of executing a foreach statement in PHP, the first item of the array is placed in the variable following the ‘as’ keyword. Whenever the control flow passes to the foreach, the subsequent array element is arranged beside the ‘as’  keyword.

Example of foreach loop

There the two types of implementations of foreach loop.

  • foreach loop to loop over an array
  • foreach loop with keys and values in an array

Simple Array

<?php
$arr=array('cat','lion','tiger','leopard');
foreach($arr as $name)
{
echo $name.'<br/>';
}
?>

Output:

cat
lion
tiger
leopard

Numerically indexed Array with keys and values

<?php
$arr=array('cat','lion','tiger','leopard');
foreach($arr as $key => $name)
{
echo $key. ' - '.$name.'<br/>';
}
?>

Output:

0 - cat
1 - lion
2 - tiger
3 - leopard

Associative Array with keys and values

<?php
$assoc=array('Amphibian'=>'Frog','Reptile'=>'Crocodile','Mammal'=>'Monkey','Aves'=>'Owl');
foreach($assoc as $key => $name)
{
echo "$name => $key <br/>";
}
?>

Output:

Frog => Amphibian 
Crocodile => Reptile 
Monkey => Mammal
Owl => Aves

Key Differences Between For and Foreach in PHP

  1. The for and foreach are the types of loops used for the different purposes in PHP where foreach is used for implementing associative arrays and for is used with variables.
  2. The for loop works by the end of the loop condition. On the other hand, the foreach loop works at the end of the array count.
  3. There is only one way to employ for loop. As against, foreach loop can be implemented in more than one way.

Conclusion

Both for and foreach loops are used for iterative functions where foreach is specially designed for the arrays while for can normally work with variable and set of a variables.

Related Differences:

  1. Difference Between for and while loop
  2. Difference Between while and do-while Loop
  3. Difference Between PHP and Python
  4. Difference Between break and continue
  5. Difference Between Recursion and Iteration

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy