Loop
What is loop?.

Loop execute some statement many times till the condition is fullfill.

loop has three type. these loops do same work execute statement many times.
1.While loop
2.Do while loop
3.For loop

while loop

In the while loop it first check the condition then body of the loop will be executed many times till the condition is fullfill.
Syntax:
while (condition)
 {
  body of code;
 }

Example 1:- This loop simple print 10 number.

<?php
$i=1;
while($i<=10)
  {
  echo $i . " ";
  $i++;
  }
?>

Output:-
1 2 3 4 5 6 7 8 9 10

do while loop

<?php
$i=1;
do
  {
  echo $i . " ";
  $i++;
  }while($i<=10);
?>

Output:-
1 2 3 4 5 6 7 8 9 10

for loop

In For loop then statement will be excuted till the condition is fullfill.
for (initilization; condition; increment)
{
  body code;
}

Example 3:- This loop simple print 10 number.

<?php
for ($i=1; $i<=10; $i++)
  {
  echo $i . " ";
  }
?>

Output:-
1 2 3 4 5 6 7 8 9 10

Example 4:- PHP script to print table and number is input by user.

<html>
<head>
<title>PHP Example</title>
<?php
if(isset($_POST['btnsubmit']))
{
$n=$_POST['number'];
$i=1;
while($i<=10)
{
echo $i*$n . "<br />";
$i++;
}
}
?>
</head>
<body>
<form name="frm" action="" method="post">
Enter Number<input name="number" type="text"/><br />
<input name="btnsubmit" type="submit" value="Submit" />
</form>
</body>
</html>


Previous
Next