Loop : In normal language that means repeat some steps.
In your program when you want to execute same statement so many times at that time you have to use this task.
In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
The while loop :
The syntax of while loop is :
While (your condition)
{
your code to be executed;
}
Lets understand while loop with example :
$i=0;
while($i<5)
{
echo "Php while loop";
$i++;
}
The output of the above program is :
Php while loop
Php while loop
Php while loop
Php while loop
Php while loop
Keep in mind when the condition becomes false the loop will terminate.
Because initial value of i is 0 so first time condition is satisfied (o<5) then it will print php while loop
After that line variable i is increment by 1 .
Now again the condition is check that is (1<5) condition is true ...these process will repeat until the value of i is 5 .
When the value of i is 5 the condition will not satisfied so the while loop is terminate.
The do...while Statement
The syntax of while loop is :
do
{
your code to be executed;
}
While (your condition);
Lets understand do while loop with example :
$i=0;
do
{
echo "Php while loop";
}
while($i<0); the output of the above program is :
Php while loop
Confuse !!!
Ok..Lets understand it .
Keep in mind in do while loop condition is satisfied or not the statement you have written in do block it will execute atleast one.
If the condition is satisfied it will execute again in short until the condition is false.
In above example the initial value of i is 0 .
When your program will execute the statement in do block that will print after that it will go for check the condition .
But the condition is ($i<0) means (0<0) so..The condition becomes false and that loop will terminate. Because of this reason do while loop is also called Exit Control Loop and while loop is Entry Control Loop.
The For Loop
The syntax of for loop is
for(init;condition;increment/decrement)
{
your code ;
}
Parameters :
Init : init means you can initialize your counter variable .
Here you can initialize multiple counter variable by " ," separte not ";".
Here after ";" it will consider it as a second parameter.
Condition : You can set the condition.
Increment / Decrement : You can increase the value of your counter variable or decrease value of counter variable.
Lets discuss an Example :
for($j=0;$j<5;$j++)
{
echo "Hi";
}
the o/p is :
Hi
Hi
Hi
Hi
Hi
The for loop is also called Entry Control Loop
The foreach loop
The foreach loop is used to loop through arrays.
The syntax of for loop is
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
$x=array("test","check","double");
foreach ($x as $value)
{
echo $value . "
";
}
The output is
test
check
double
In your program when you want to execute same statement so many times at that time you have to use this task.
In PHP, we have the following looping statements:
while - loops through a block of code while a specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
The while loop :
The syntax of while loop is :
While (your condition)
{
your code to be executed;
}
Lets understand while loop with example :
$i=0;
while($i<5)
{
echo "Php while loop";
$i++;
}
The output of the above program is :
Php while loop
Php while loop
Php while loop
Php while loop
Php while loop
Keep in mind when the condition becomes false the loop will terminate.
Because initial value of i is 0 so first time condition is satisfied (o<5) then it will print php while loop
After that line variable i is increment by 1 .
Now again the condition is check that is (1<5) condition is true ...these process will repeat until the value of i is 5 .
When the value of i is 5 the condition will not satisfied so the while loop is terminate.
The do...while Statement
The syntax of while loop is :
do
{
your code to be executed;
}
While (your condition);
Lets understand do while loop with example :
$i=0;
do
{
echo "Php while loop";
}
while($i<0); the output of the above program is :
Php while loop
Confuse !!!
Ok..Lets understand it .
Keep in mind in do while loop condition is satisfied or not the statement you have written in do block it will execute atleast one.
If the condition is satisfied it will execute again in short until the condition is false.
In above example the initial value of i is 0 .
When your program will execute the statement in do block that will print after that it will go for check the condition .
But the condition is ($i<0) means (0<0) so..The condition becomes false and that loop will terminate. Because of this reason do while loop is also called Exit Control Loop and while loop is Entry Control Loop.
The For Loop
The syntax of for loop is
for(init;condition;increment/decrement)
{
your code ;
}
Parameters :
Init : init means you can initialize your counter variable .
Here you can initialize multiple counter variable by " ," separte not ";".
Here after ";" it will consider it as a second parameter.
Condition : You can set the condition.
Increment / Decrement : You can increase the value of your counter variable or decrease value of counter variable.
Lets discuss an Example :
for($j=0;$j<5;$j++)
{
echo "Hi";
}
the o/p is :
Hi
Hi
Hi
Hi
Hi
The for loop is also called Entry Control Loop
The foreach loop
The foreach loop is used to loop through arrays.
The syntax of for loop is
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
$x=array("test","check","double");
foreach ($x as $value)
{
echo $value . "
";
}
The output is
test
check
double
Hey ...
ReplyDeleteThank you for this post.
It is really very needful for me.
http://www.codeworld.co.cc
ReplyDelete