PHP While Loop
Posted on: February 15, 2010 at 12:00 AM
Advertisement
In the current tutorial we will study while loop statement in PHP. In programming language we need to use loops for executing a set of instruction more than one time, there are mainly three types of loops are used namely for, while and do while. Apart from these there are some loops are used in different languages like until, for each etc.

While Loop in PHP:

In programming language we need to use loops for executing a set of instruction more than one time, there are mainly three types of loops are used namely for, while and do while. Apart from these there are some loops are used in different languages like until, for each etc.

In the current tutorial we will study while, do while and for.

So let's get started:

While Loop in PHP:

While loop checks the condition first and if it is true then it proceeds and allows to execute the codes written within the block and keep executing the codes until the condition becomes false, sometimes it fails to meet the condition to stop, and become infinite loop. While loop is also known as pre-test loop.    

PHP While Loop Example:

<?php

$i=1;

while($i<11){

echo "Value of \$i is $i <br/>";

$i++;} 

?>

Output:

Value of $i is 1
Value of $i is 2
Value of $i is 3
Value of $i is 4
Value of $i is 5
Value of $i is 6
Value of $i is 7
Value of $i is 8
Value of $i is 9
Value of $i is 10

As in the given example we can see that we need to declare and initialize a variable before using that, while loop checks the condition and if it is true then it proceeds to the next set of coding otherwise it wont . Inside  the braces we need to change the value of the variable otherwise condition remains same and it will execute indefinite times, that's why we should change it either by increasing or decreasing value. When the condition become false iteration will be stop and exits from the loop.

Advertisement

Related Tags for PHP While Loop:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com