Loops are used to repeatedly execute the same block of code as long as a condition is met. The basic concept behind a loop is to save time and effort by automating repetitive activities within a program. There are four different types of loops supported by PHP.

Types of Loop in PHP

while — loops through a block of code until the given condition evaluates to true.

do...while — the code block is run once, and then the condition is evaluated. If the condition is valid, the sentence will be repeated until the condition is no longer true.

for — iterates over a block of code until the counter exceeds a certain value.

foreach — for each element in an array, loops through a block of code.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Understanding What Is For Loop

For loop is a control structure that repeats a block of code as long as a condition is met. It's usually used to repeat a block of code a certain number of times.

The PHP for loop function can be used to iterate over a set of code for a set number of times.

If the number of iterations is specified, it should be used; otherwise, a while loop should be used. When I already know how many times it wants to execute a block of code, use a for loop.

It helps users to centralize all loop-related statements.

Importance of Looping Statements

Since the condition is checked at the end of the loop iteration rather than the beginning, a do-while loop will always be executed once, even if the conditional expression is false.

In PHP, a loop and for loop is used to execute a statement or a block of statements several times until and until a certain condition is met, much as in any other language. This saves the user time and effort from having to write the same code several times.

The following are the definitions of the for loop parameters:

Initialization:

Initialization is used to set the counter variables, and it is checked once unconditionally before the loop's body is executed.

State:

State — condition is assessed at the start of each iteration. The loop continues if it evaluates to true, and the nested statements are executed. If it evaluates to false, the loop is terminated.

Which is a second argument and also called a conditional checking part.

Increment:

Increment — it adds a new value to the loop counter. At the end of each iteration, it is evaluated.

Syntax:

for ( initializing value; condition checking; increment)

{

  Code to be executed

}

Or

for (init counter; test counter; increment counter)

 {

  code to be executed for each iteration;

}

Code:

Following example demonstrates the simple usage of for loop

<html>

<body>

<?php

$d = 0;

$e = 0;

for( $i = 0; $i<5; $i++ )

{

$d += 10;

$e += 5;

}

echo ("At the end of the loop d = $d and e = $e" );

?>

</body>

</html>

Output:

For_Loop_in_PHP_1 

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Init Counter

Which is used in the loop counter to initialize the value, it will be executed only once.

For Each Loop Iteration

The test counter is evaluated. The loop continues if it evaluates to TRUE. The loop ends if it evaluates to FALSE.

Increment Counter:

Increases the value of the loop counter.

 Code:

<!DOCTYPE html>

<html>

<body>

<?php 

for ($no = 0; $no <= 10; $no++) {

  echo "The number is: $no <br>";

}

?>

</body>

</html>

Explanation:

for ($no = 0; $no <= 10; $no++)

In the above piece of code the first argument

$no = 0;

Which belongs to init counter

Second argument

$no <= 10

Which belongs to for loop iteration

Third argument

$no++

Which belongs to Increment counter

Output:

For_Loop_in_PHP_2.  

While Loop:

The while statement loops around a block of code as long as the while statement's condition evaluates to true.

Syntax:

while(condition){

// Statements to be executed

}

Code:

<!DOCTYPE html>

<html>

<body>

<?php 

$s = 1;

while($s <= 7)

 {

  echo "The number is: $s <br>";

  $s++;

}

?>

</body>

</html>

Output:

  For_Loop_in_PHP_3

Do… While Loop:

The while loop is a version of the do-while loop in which the condition is evaluated at the end of each loop iteration. A do-while loop executes a block of code once, then evaluates the condition; if the condition is true, the statement is repeated as long as the stated condition is true.

Syntax:

Do

 {

Statements to be executed;

} while (condition is true);

Code:

<!DOCTYPE html>

<html>

<body>

<?php

$a = 1;

do {

  echo "The number is: $a <br>";

  $a++;

} while ($a <= 10);

?>

</body>

</html>

Output:

/For_Loop_in_PHP_4

Difference Between While Loop and Do… While Loop

The while loop differs from the do-while loop in one significant way: with a while loop, the condition to be evaluated is checked at the start of each loop iteration, so the loop can never be executed if the conditional expression evaluates to false.

Since the condition is checked at the end of the loop iteration rather than at the beginning, a do-while loop will always be executed once, even if the conditional expression is false.

Learn From The Best Mentors in the Industry!

Automation Testing Masters ProgramExplore Program
Learn From The Best Mentors in the Industry!

Foreach Loop

Iterating over arrays is done with the foreach loop.

Syntax:

foreach($array as $value)

{

// Statements to be executed

}

Code:

<?php

$colors = array("Red", "Green", "Blue","Yellow","Pink");

// Loop through count array

foreach($colors as $value){

echo $value . "<br>";

}

?>

Output:

/For_Loop_in_PHP_5 

Conclusion

Foreach loops in PHP have a specific function. They're designed to work with PHP variables that are more complex, such as arrays and objects. Using foreach, PHP code blocks will be run multiple times, each with its own piece.

Now, PHP for loop is used to run code segments multiple times. It may determine the number of times the execution will take place. As a result, this loop is ideally suited to cases where you know exactly when the code block needs to run. A loop runs a given number of times regardless of the conditions.

Learn how to do all this yourself by joining our Simplilearn’s Postgraduate Program in Full Stack Web Development. Empower yourself by joining this Post Graduate Program and start your career as a full stack developer. You can also explore Simplilearn’s free courses here. Find well-designed free guides, access the free study materials on technology, and come out with flying colours.

Have any questions for us? Leave them in the comments section of this article, and our experts will get back to you on them, as soon as possible!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449