PHP While Loop


php PHP While Loop While Loop

While Loop and its execution in PHP

Basically, while loop executes the statement until some condition become true. This loop executes one or more statements while the given condition remains true. It is useful when number of iteration (cycle) is not known in advance.

While Loop Syntax

The syntax of while loop is as follow: while(condition) statement;

Working of While Loop

First of all, the condition is evaluated. If it is true , the control enters the body of the loop and executes all statements in the body. After execution the statements, it again moves to the start of the loop and evaluates the condition again. This process continues as long as the condition remains true.

While Loop Flow chart

Program to display any sentence 3 times using while loop

Write a sentence "Princes of courtesy, merciful, proud and strong." 3 times using while loop.

<?php $num = 1; while($num<=3) { echo "Princes of courtesy, merciful, proud and strong."."<br>"; $num++; } ?> Output: Princes of courtesy, merciful, proud and strong. Princes of courtesy, merciful, proud and strong. Princes of courtesy, merciful, proud and strong.
php While Loop Chapter Next »