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
Comparison Chart
Basis for comparison | For | Foreach |
---|---|---|
Implemented over | Variable/s | Numerical and associative arrays |
Working | At the end of the given condition | At 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
- 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.
- 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.
- 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.
Leave a Reply