Home » PHP

Loop statements in PHP with examples

PHP loop statements: In this article, we are going to learn about the various loop statements in PHP programming language with examples.
Submitted by Kongnyu Carine, on May 21, 2019

Loops

Imagine that we need a program that says "hello world" 100 times. It's quite stressful and boring to write the statement -- echo "hello world" — 100 times in PHP. This is where loop statement facilitates the work for us.

A loop statement is a statement that execute as long as a particular condition is valid and stops when that condition is invalid.

Let's look at the different loops in PHP.

1) The while loop

The while statement executes a particular block of code as long as a statement remains true.

Syntax:

    while (condition true) {
         code to be executed;
    }

Example:

We want to display the number 1 to 5.

<?php
$x = 1;
while ($x <= 5) {
    echo "Number is: $x <br>";
    $x++;
}
?>

Output

Number is :1
Number is :2
Number is :3
Number is :4
Number is :5

2) The do...while loop

The do...while loop is the same as the while loop but for that it executes your code atleast once even if the condition is false before checking the condition to true and continues executing as the statement remains true.

Syntax:

    do {
        code to be executed;
    } while (condition is true);

Example:

In this example we will repeat the example above but demonstrate how the do..while loop executes your code atleast once whether true or false before checking the condition.

<?php
$x = 1;
do {
    echo "Number is: $x <br>";
    $x++;
} while ($x >= 5);
?>

Output

Number is :1

3) The for loop

The for loop works as the while loop but a difference in syntax, in this loop all the things like counter initialization, condition, increment and decrement statements are placed together separated by the semicolon.

Syntax:

    for (initialization counter; test counter; increment/decrement  counter) {
        code to be executed;
    }

The initialization counter is used to set the initial value.

Test counter or condition determines the execution process, if true the loop continues if false the loop stops.

The increment/decrement counter, used to increments or decrements the initial value.

Example:

We go with our example again listing numbers from 1 to 5 with the for loop

<?php
for ($x = 1;$x <= 5;$x++) {
    echo "Number is: $x <br>";
}
?>

Output

Number is :1
Number is :2
Number is :3
Number is :4
Number is :5





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.