PHP Loop
Loop execute some statement many times till the condition is fullfill.
There are three type or loops. these loops do same work execute statement many times.
There are three type or loops. these loops do same work execute statement many times.
- While loop
- Do While loop
- For Loop
while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is:
===========================
while (expr)
statement
===========================
In the while loop it first check the condition then body of the loop will be executed many times untill the condition is fullfill.
Syntax
===========================
while (condition)
{
Code here.
}
===========================
Exercise 1:
===========================
<?php
$i=1;
while($i<=5)
{
echo "Number".$i;
$i++;
}
?> Output
Number 1
Number 2
Number 3
Number 4
Number 5
do while
Exercise 2:
===========================
<?php
$i=1;
do
{
echo "Number".$i;
$i++;
}while($i<=5)
?>
Output
Number 1
Number 2
Number 3
Number 4
Number 5
===========================
For loop
In For loop then statement will be excuted till the condition is fullfill.
===========================
for (initilization; condition; increment)
{
code here...;
}
===========================
Exercise 1:
<?php
for ($i=1; $i<=5; $i++)
{
echo "Number".$i;
}
?>
Output
Number 1
Number 2
Number 3
Number 4
Number 5
Syntax
===========================
while (condition)
{
Code here.
}
===========================
Exercise 1:
===========================
<?php
$i=1;
while($i<=5)
{
echo "Number".$i;
$i++;
}
?> Output
Number 1
Number 2
Number 3
Number 4
Number 5
do while
Exercise 2:
===========================
<?php
$i=1;
do
{
echo "Number".$i;
$i++;
}while($i<=5)
?>
Output
Number 1
Number 2
Number 3
Number 4
Number 5
===========================
For loop
In For loop then statement will be excuted till the condition is fullfill.
===========================
for (initilization; condition; increment)
{
code here...;
}
===========================
Exercise 1:
<?php
for ($i=1; $i<=5; $i++)
{
echo "Number".$i;
}
?>
Output
Number 1
Number 2
Number 3
Number 4
Number 5
0 (mga) komento:
Post a Comment