Skip to main content

PHP Loops

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

Comments

  1. Hey ...
    Thank you for this post.
    It is really very needful for me.

    ReplyDelete

Post a Comment

Popular posts from this blog

Number Format In Javascript

Number.toFixed() :                                 Formats any number for "x" number of trailing decimals. The number is rounded up, and "0"s are used after the decimal point if needed to create the desired decimal length.   var profits=2489.8237 profits.toFixed(3) //returns 2489.824 (round up) profits.toFixed(2) //returns 2489.82 profits.toFixed(7) //returns 2489.8237000 (padding)   Number.toPrecision() :                                   Formats any number so it is of "x" length. Also called significant digits. A decimal point and "0"s are used if needed to create the desired length.   var anumber=123.45 anumber.toPreci...

Use of indexOf and charAt in javascript

indexOf(): The indexOf() method returns the position of the first occurrence of a specified value in a string. This method returns -1 if the value to search for never occurs. example :  <script type="text/javascript"> var str="Hello world!"; document.write(str.indexOf("d") + "<br />"); document.write(str.indexOf("WORLD") + "<br />"); document.write(str.indexOf("world")); </script> Output : 10 -1 6 charAt() The charAt() method returns the character at the specified index in a string. The index of the first character is 0, and the index of the last character in a string called "txt", is txt.length-1 <script type="text/javascript"> var str = "Hello world!"; document.write("First character: " + str.charAt(0) + "<br />"); document.write("Last character: " + str.charAt(str.l...

Find extension of file in Php

In our scripting sometimes we need to upload such documents , at that time there should be a validation for proper file. For example in your system suppose there is a field for image upload , that is obvious in that field only image formats are allowed (jpg,jpeg,png,gif etc.). At that time you have to give a validation on that field. In Php to find the extension and validation here is your code :  <?php                     $image = $_FILES["file"]["name"];    // to fetch the file name             $n1=strripos($image,".");                     // to find the last position of "."             $v1 =substr($image,$n1);                  ...