Difference Between For and Foreach in PHP


In this post, we will understand the differences between 'for' and 'foreach' loops in PHP −

The 'for' loop

It is an iterative loop that repeats a set of code till a specified condition is reached. It is used to execute a set of code for a specific number of times. Here, the number of times is the iterator variable.

Syntax:

for( initialization; condition; increment/decrement ) {
   // code to iterate and execute
}
  • Initialization: It is used to initialize the iterator variables. It also helps execute them one at a time without running the conditional statement at the beginning of the loop's condition.
  • Condition: This statement is executed and if the condition returns a True value, the loop continues and the statements within it are executed. If the condition gives a False value, the execution comes out of the loop.
  • Increment: It increments/increases the counter in the loop. It is executed at the end of every iteration without a break.
  • It doesn't hide the iteration.
  • It is complex in comparison to 'foreach' loop.
  • It takes more time to execute in comparison to 'foreach' loop.

Let us see an example −

<?php
for($i = 1; $i <= 2; $i++) {
   echo $i . " Hi 
"; } ?>

The 'foreach' loop

  • It iterates over the elements of the array data structure.
  • It hides the iteration.
  • It is simple.
  • It performs better in comparison to 'for' loop.
  • It takes less time for the iteration.

Syntax:

foreach( $array as $element ) {
   // PHP Code to execute
}
foreach( $array as $key => $element) {
   // PHP Code to execute
}

Example

<?php
$peop = array( "Will", "Jane", "Harold" );
foreach( $ peop as $element ) {
   echo $element . "<br>";
}
?>

Output

Will
Jane
Harold

Conclusion

In this post, we understood about the significant differences between the 'for' and 'foreach' loop in PHP.

Updated on: 02-Mar-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements