Performance of for vs foreach in PHP - GeeksforGeeks

15 Jul, 2021
for Loop: The for loop is an iterative loop which repeats a certain set of code until a specified condition is reached. It is systematically used to execute a set of code for specified number of times, here the number of times denotes the iterator variable. 
Syntax:  
for( initialization; condition; increment/decrement ) {
    // Set of Code to be iterated and executed
}
The each parameters of for loop have the unique functionalities which are explained below for better understanding: 
initialization: It is used to initialize the iterator variables, and executed at one time without running the condition statement during the start of the looping condition that is the very first execution of the set of code in the loop.
condition: In the beginning of each iteration, condition statement is executed and if the condition returns a true value, the loop continues and the nested statements in the set of code are executed. If the condition evaluates to be false, the execution of the loop gets break at that point of code.
increment: It increases the loop counter with a new increment value that is to be evaluated for the condition statement. It is compulsorily executed at the end of each iteration without any break.
Example: This example uses a for loop that starts with $j = 1. The loop will continued until $j is less than, or equal to 5. The variable $j will increase by 1 each time the loop runs.  
php
<?php // Loop starts from herefor($j = 1; $j <= 5; $j++) {    echo $j . " GeeksforGeeks \n";} ?>
1 GeeksforGeeks 
2 GeeksforGeeks 
3 GeeksforGeeks 
4 GeeksforGeeks 
5 GeeksforGeeks
 
foreach Loop: The purpose of foreach loop is to define the use and it specifically iterate over the elements of the array data structure.Syntax:  
foreach( $array as $element ) {
    // PHP Code to be executed
}
 
foreach( $array as $key => $element) {
    // PHP Code to be executed
}
Below examples illustrate the use of foreach loop. 
Example 1: 
php
<?php$students = array( "Jimmy", "Jonny", "Jacky" ); // Loop through students arrayforeach( $students as $element ) {    echo $element . "<br>";} ?>
Jimmy
Jonny
Jacky
 
Example 2:  
php
<?php$employee = array(    "name" => "Robert",    "email" => "robert112233@mail.com",    "age" => 18,    "gender" => "male" ); // Loop through employee arrayforeach($employee as $key => $element) {    echo $key . ": " . $element . "<br>";} ?>
name: Robert
email: robert112233@mail.com
age: 18
gender: male
 
Performance comparison in for and foreach loop:  
The for loop is considered to be openly executing the iteration where as the foreach loop hides the iteration and visibly simplified.
The foreach loop is considered to be much better in performance to that of the generic for loop.
The foreach loop though iterates over an array of elements, the execution is simplified and finishes the loop in less time comparatively.
The foreach loop allocates temporary memory for index iterations which makes the over all system to redundant its performance in terms of memory allocation.
Example:  
php
<?php// PHP program to compare the performance// of for and foreach loop // Declarations$units = array(); // An array of 100 units with random string valuesfor($i = 0; $i <= 100; $i++) {    $units[] = (string)rand(11111, 99999);} $startingTime = microtime(true); // for loop performance evaluation$size = count($units);for($i = 0; $i < $size; $i++) { } $endingTime = microtime(true);$forlooptime = $endingTime - $startingTime; $startingTime = microtime(true); // foreach loop performance evaluationforeach($units as $unit) { } $endingTime = microtime(true);$foreachlooptime = $endingTime - $startingTime; echo "for loop evaluates to: ". number_format($forlooptime * 1000, 6); echo "\nforeach loop evaluates to: "    . number_format($foreachlooptime * 1000, 6); ?>
for loop evaluates to: 0.002861
foreach loop evaluates to: 0.002861
geeksforgeeksorg20
Marketing
Picked
PHP
PHP Programs
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
PHP str_replace() Function
How to pass form variables from one page to other page in PHP ?
Create a drop-down list that options fetched from a MySQL database in PHP
How to call PHP function on the click of a Button ?
How to fetch data from localserver database and display on HTML table using PHP ?
How to create admin login page using PHP?
How to pass form variables from one page to other page in PHP ?
PHP | Ternary Operator