Output

Looping
In the previous section you learned about the conditional statements. In this section you will be learning about the looping structures. The looping statements are used to execute a same block of code a specified number of times.
The various looping statements available in PHP are:
While – it loops through a block of code if a specific condition is true and it comes out of the loop only when that condition becomes false.
Do while – in this it loops through a block of code at least once, then it checks for the condition and loops as long as that condition is true.
For – it loops through a block of code a specified number of times.
Foreach – it loops through a block of code for each element in an array. While Loop
The “while” loop, is the simplest type of loop in PHP. They work the same in PHP as they do in other languages. The “while” statement executes a block of code if a specific condition is true and it comes out of the loop only when that condition becomes false.
| Syntax |
while (condition)
code to be executed; |
| |
In the following example we are using the while loop which checks for the condition and executes the loop till the condition is true.
Example |
<html>
|
<body> |
|
<?php
|
$a=1;
|
while($a<=4)
|
{
|
echo "Welcome to floor " . $a ."<br/>";
|
$a++;
|
}
|
?>
|
</body>
|
</html> |
| |
Output:

|