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.

Post Graduate Program: Full Stack Web Development

in Collaboration with Caltech CTMEEnroll Now
Post Graduate Program: Full Stack Web Development

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 

New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.

Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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 

Advance your career as a MEAN stack developer with the Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

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!

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors