Loops in PHP

Loops in PHP

loops are by far the most powerful component of php. aA loop is simply a set of statements repeated more than once.

There may be a situation when you need to execute a block of code several number of times. But when you are running the same code over and over would produce the same result. So what is the benefit of using Loop in that case?  Basically a loop contains a variable that changes slightly each time.

 

The For Loop

PHP for loop can be used to traverse set of code for the specified number of times. For example if you want to repeat something ten or twenty times.

It should be used if number of iteration is known otherwise I recommend you to use while loop.

Syntax

 for (Initialization; Condition; Progressive)

{

//set of statements to be executed;

}

Parameters:

Initialization: Initialize the loop counter value

Condition: Check for condition. If it evaluates to TRUE, the loop continues. If FALSE then the loop ends.

Progressive: Increase / Decrease the loop counter value

 Example:

 for($var=1; $var<=20; $var=$var+1)

{

echo $var .” ”;

}

Output:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

 Example to display 1st 20 natural numbers in drop-down list

 <?php

echo “<select>”;

 for($i=1; $i<=20; $i++)

{

echo “<option>”.$i. “</option>”;

}
echo “</select>”

?>

 Please note inside loop area we are using variable $i to display values in drop-down list as we use variable that changes slightly each time

The Nested For Loop: We can use for loop inside another for loop in PHP, it is known as nested for loop. In case of inner or nested loop, nested for loop is executed fully for one outer for loop.

 Example, If outer loop is to be executed for 2 times and nested Inner loop for 2 times, then the nested inner loop will be executed 4 times (2 times for 1st outer loop and 2 times for 2nd outer loop).

Example:

 <?php

for($i=1; $i<=5; $i++)
{
for($j=1; $j<=$i; $j++)
{
echo $j. " ";
}
echo "<br>";
}

?>

 Output

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

The While loop

A While loop is used when we have to repeat a set of statements as long as the condition is false. It could be the best where number of repetition is not known earlier or in advance.

It can be explain in plain English as “Keep doing something until a condition is true”.

Syntax

while (condition)

{

//  Set of statements goes here;

}

Example

 <?php
$ct=5;
while($ct<=50)
{
echo  $ct . "<br>";
$ct = $ct+5;
}
?>

 Output:

5 10 15 20 25 30 35 40 45 50
The do…while loop example:

<?php
$i = 1;
do
{
 $i++;
    echo  $i . "<br>";
}
while($i <= 3);
?>
Output:
 2 3 4

 Difference Between while and do…while Loop: With a while loop, the condition to be evaluated is tested at the beginning of each loop statement, so if the conditional expression evaluates to false, the loop will never be executed.

With a do-while loop, on the other hand, the loop will always be executed once, even if the conditional expression is false, because the condition is evaluated at the end of the loop statement.

 Switch Statement in PHP

The switch statement is similar to a series of if statements on the same expression.

<?php

$i=0;

switch ($i)

{

case 0:

echo “$i equals 0″.”<br/>”;

break;

case 1:

echo “$i equals 1″.”<br/>”;

break;

case 2:

echo “$i equals 2″.”<br/>”;

break;

}

?>

Another Example:

<html>

<form method=”post”>

Enter first number<input type=”text” name=”f”/><br/>

Enter second number<input type=”text” name=”s”/><br/>

Enter your choice<input type=”text” name=”ch”/><br/>

<input type=”submit” value=”Calculate Now”/>

</form>

<?php

$f=$_POST[‘f’];

$s=$_POST[‘s’];

$choice=$_POST[‘ch’];

switch ($choice)

{

case “+”:

$sum=$f+$s;

echo “Sum=”.$sum;

break;

case “-“:

$sub=$f-$s;

echo “Subtraction=”.$sub;

break;

case “*”:

$mult=$f*$s;

echo “Multiplication=”.$mult;

break;

case “/”:

$div=$f/$s;

echo “Division=”.$div;

break;

default:

echo “Invalid choice”;

}

?>

</html>

Enjoy the tutorial? Please spread the word :)

Follow by Email
Facebook
Facebook
Google+2k
Google+
http://hirdeshbhardwaj.com/2017/08/10/loops-in-php/
YouTube31
YouTube