In PHP Scripting language we used loops for executing the same block of code a specified number of times. In this language BREAK and CONTINUE keywords are used to control loop execution. 
In PHP, there are four loops that support programming.

For Loop: - The for loop is used in PHP when you know about how many times you want to execute a statement or block of statement and how many time script should be run. Simply, this loops through a block of code a specified number of times.
Syntax:   for (initialization; condition; increment ;)
	     {
 		     Code to be executed;
		}
Here, initialize set the start value for the counter of the number of loop. We have declared a variable named $i
Example: How can we make five iterations and change the assigned value of two variables in each by passing loop.
<-?php
$y = 0;
$z = 0;
    For ( $i=0; $i<5; $i++ )
       {
         $a  += 10;
         $b  += 5;
         }
    echo (“At the end of the loop a = $a and b = $b” );
?->

At the end of the loop the output will be as follow:

a=50 and b=25
While Loop: - The while loop is used in PHP to execute the block of code as long as test expression is true. If test expression is find true the loop executed the block of code.  After complete the code execution the expression will be evaluate again and the loop will continue until the false test expression is to be found. Simply this loop through a block of code as long as a specified condition is true.
Syntax:  while (condition);
	      {
                 Code to be executed;
                }
Example:  How can be decrease a variable value on each iteration of loop and the counter increases until it arrives 10. The loop will be end when the evaluation is false.
<-?php
$a = 0;
$num = 50;
while ( $a < 10 )
{
    $num--;
    $a++;
}
echo ( “Loop stopped at a = $a and num = $num );
?->

At the end of loop the output will be as follow:
a = 10 and num = 40
Do While: - The do…..while loop is used in PHP to execute a block of code at least once and then the loop will repeat as long as the test expression is true. Simply, this loop through a block of code then repeats as long as the specified condition is true.
Syntax:  do
              {
                     Code to be executed;
              } while (condition);

Example:  How can be increase the value of variable at least once and will continue increasing the variable as long as its value of less than 10.
<-?php
$a = 0;
$num = 0;
do
{
    $a++;
} while ( $a < 10 );
echo ( “Loop stopped at a = $a” );
?->
Foreach Loop: - The foreach statement pass the value of current array element is assigned to $array and the array pointer are moved by one and the next value passes the next element. Simply this loop through a block of code for each element in an array.
Syntax:  foreach (array as value)
              {
                        Code to be executed;
              }

Example: How can be list out the values of an array?
<-?php
$array = array (1, 2, 3, 4, 5);
Foreach ($array as $value)
{
    echo “value is $value”;
}
?->

The output will be as follow:
Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Break Keyword: In PHP the break statement is used for terminating the execution of loop prematurely. This keyword is situated inside the statement block. It provides full control to the programmer. With using break keyword you can came out the loop whenever you want to exit. An immediate statement to the loop will be executed after coming out of loop.

Example:  In this example the test expression will be true when counter value reaches 5 then loop will be terminate.
<-? Php
$a = 0;
While ( $a < 10 )
{
     $a++;
     if ( $a == 5 ) break ;
}
echo   “(Loop stopped at a = $a)”;
?->

The output will be as follow:
Loop stopped at a = 5

Continue Keyword: In PHP the continue keyword is using for halting the current iteration of loop. It’s not a kind of terminating. The continue statement is situated inside the block statement block. It contains the code which proceeded by conditional test and executed by loop. By passing encountering continue keyword rest of the loop code is skipped and next pass will be start.

Example:  In this example loop prints the value of array for true condition then it just skips the code and next value will be print.
<-?php

$array = array( 1, 2, 3, 4, 5);

foreach( $array as $value )

{

  if( $value == 3 )continue;

  echo "Value is $value";

}

?->


The output will be as follow:
Value is 1
Value is 2
Value is 4
Value is 5
If you Still Facing any problem then feel free to contact me through mail or contact form below.

2 comments :

 
# Top