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++.

Please Share This Article with Friends
Fazal Rehman Shamil
Welcome to all friends. The reason for our success is only your love for T4Tutorials. Our team is always available to answer your queries regarding any kind of confusions or discussion regarding your study and career matters. For discussion with us please join our facebook group "T4Tutorials.com". The link of the group is mentioned below. Thanks and love to all for connecting with us. We are nothing without you. Love you all.....
https://web.facebook.com/groups/2066136233601097/