In this tutorial we will learn how to use while loop with working examples.
Looping: looping is also iteration. It is the ability to perform the same set of operations repeatedly till a specific condition is met when control comes out from the loop.
while () loop: The while is an entry –controlled loop statement. The test-condition is evaluated and if the condition is true, then the body of loop is executed. It is a pretested loop. The basic format of while statement is-
while(test condition)
{
Body of the loop
}
Consider the following example:
pro1.php
<?php
$i=1;
while($i<=5)
{
echo “Welcome”.”<br>”;
$i++;
}
?>
Que: program of table using while loop.
pro2.php
<?php
$n=5;
$i=1;
while($i<=10)
{
$t=$n*$i;
echo $n.” X “.$i.” = “.$t.”<br>”;
$i++;
}
?>
Consider the following example. In this program a table will print which is of 5 rows and 3 columns.This table created using HTML.
pro3.php
<table border=”1″ bgcolor=”#669966″>
<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>
<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>
<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>
<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>
<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>
</table>
In the above example pro3.php is now modified with the help of PHP and HTML in pro4.php. So save run and try to understand it.
pro4.php
<?php
echo”<table border=’1′ bgcolor=’#669966′>”;
while($i<=5)
{
echo”<tr><td>Hello</td><td>Hello</td><td>Hello</td></tr>”;
$i++;
}
echo”</table>”;
?>
Now practice some example which is describe in following section question by question.
pro5.php
<?php
if(isset($_POST[‘b1′]))
{
echo”<table border=’1’>”;
$i=1;
$n=$_POST[‘t1′];
while($i<=10)
{
$t=$n*$i;
echo”<tr><td>$n</td><td>X</td><td>$i</td><td>=</td><td>$t</td></tr>”;
$i++;
}
echo “</table>”;
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
<input type=”submit” name=”b1″ value=”Print Table”>
</form>
In the following program a list box is created using HTML
pro6.php
Date Of Year<select>
<option value=”1960″>1960</option>
<option value=”1961″>1961</option>
<option value=”1962″>1962</option>
<option value=”1963″>1963</option>
<option value=”1964″>1964</option>
</select>
The list box which is created in pro6.php with the help of html ,now modified with the help of PHP & HTML. Try to understand.
pro7.php
Date Of Year<select>
<?php
$i=1960;
while($i<=2011)
{
echo “<option value=’$i’>$i</option>”;
$i++;
}
?>
</select>
Question: write a program to enter a number from a form and print it’s Reverse.
Solution:
pro8.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$rev=0;
$mod=0;
while($number>0)
{
$mod=$number%10;
$rev=$rev*10+$mod;
$number=(int)($number/10);//here (int) is used for type casting.
}
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
Reverse<input type=”text” value=”<?php if(!empty($rev))echo $rev;?>”><br>
<input type=”submit” name=”b1″ value=”Reverse”>
</form>
Question: Write a program to enter a number using form and print table of each digit which is not 0 and 1.
Solution:
pro9.php
<?php
if(isset($_POST[‘b1’]))
{
$number=$_POST[‘t1’];
$digit=0;
while($number>0)
{
$digit=$number%10;
if($digit!=0 && $digit!=1)
{
echo “Table of “.$digit.”<br>”;
$i=1;
while($i<=10)
{
$t=$digit*$i;
echo $digit.” X “.$i.” = “.$t.”<br>”;
$i++;
}
}
}
}
?>
<form action=”” method=”post”>
Enter Number<input type=”text” name=”t1″><br>
<input type=”submit” name=”b1″ value=”Table”>
</form>