Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

While Loop in PHP

While Loop in PHP

While loop executes a block of statements as long as the condition that is defined is true.

In the example below, we have set a variable $x to 1.The while loop will continue to run as long as the value of $x  is less than or equal to 10. Variable “$x” is increment by 1 each time the loop runs $x++.

Flowchart of While Loop. 

while loop in php
Figure: while loop in PHP.

Example of While loop in PHP.

An example program to demonstrate the while loop.

The output of While loop program in PHP.

The value is:1
The value is:2
The value is :3
The value is:4
The value is:5
The value is:6
The value is:7
The value is:8
The value is:9
The value is:10

Code

Explanation

<?phpPhp  opening tag.
  $x=1;Variable is declared and value is initialized.
 while($x<=10)Here loop checks condition and executes the block of

Statements.

 {While loop opening tag.
                 echo “The value is :$x<br>”;Code within Loop . Prints the message.
                 $x++;Value of variable $x is increment by 1.
 }Loop ends.
?>Php closing  tag.

 

PHP CodeSame code in C++
<?php
  $x=1;Int x=1;
 while($x<=10)While(x<=10)
 {
                 echo “The value is :$x<br>”;Cout <<”The value is :”<<x<<endl;
                 $x++;x++;
 }
?>

Hopefully, now you are able to understand the while loop in C++.