pTutorial

New way to learn PHP

PHP 5 Loops

What is loop?

In Technical Term, The basic idea of a loop is to run the block of code again and again until a certain condition met.

  • It is used to execute a block of code many times (as per condition). It is also a conditional statement like if-else.
  • Looping is repeating set of instruction until condition true.

Four loops statement are available in PHP

  1. For loop
  2. While loop
  3. Do While loop
  4. Foreach loop

Multiplication table using PHP loops

<?php

echo "<h1>Multiplication table</h1>";

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

{ 

echo "$var*2"."=". $var*2;

echo " 
"; } ?>

For loop

A for loop is usually used when the exact number of times a block of code must execute is known in advance.

  • The for loop is the easiest way to understand of the PHP loops.

Flow chart of for loop

for loop ptutorial

Syntax

for ( initiation ; condition ; increment / decrement )
{
code to be executed
}
  • The initialization state of a variable is setting a loop counter to an initial value.
  • It's involving the relational operator, test the loop counter to determine whether its value has reached the number of repetitions desired.
  • Increment or decrement the value of the counter each time.

For loop example

<?php

echo "simple php for loop
"; echo "Print statement"."
"; for ($var = 1; $var <= 10; $var++) { echo "$var times execute
"; } ?>

In the above example, first initialise the counter variable $var = 1 and after that tested the condition, that is true and the body of the loop is executed. Upon reaching the end of the for loop control, is sent back to the for loop, where the value of $var counter gets incremented.

This task is done again and again until the condition is false.

Nested for loop

For loop within for loop is known as nesting of for loop. You can put any number of loop within the loop.

<?php

echo "<h1>Print pattern</h1>";
  for ( $row = 1 ; $row <= 10 ; $row++ )
   {
   $temp=10;
      for ( $c = 1 ; $c < $temp ; $c++ )
         echo "  ";
 
      $temp--;
 
      for ( $c = 1 ; $c <= 2*$row - 1 ; $c++ )
         echo"*";
 
      echo "
"; } ?>

Outer loop will be executed ten times and the inner loop will be executed according to the given condition, the first inner loop will be printed the white space and second will be the star.


Break and continue in for loop

Break keyword

Sometimes we need to terminate the for loop without reaching the end of loop, break keyword is used for this type of the conditional jump following program sees the use of break keyword.

Example of switch

<?php

    for ($var = 1; $var <= 10; $var++)
	{
		echo "$var times execute
"; if($var==5) break; } ?>

Output

1 times execute
2 times execute
3 times execute
4 times execute
5 times execute

In the above example, for loop will be terminated when the $var=5 because of break keyword.

while loop loop has been explained in the next section.


QUOTES OF THE DAY

Impossible is a word only be found in the dictionary of fools.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER