PHP continue statement

PHP “continue” statement is used within the loops to skip the current loop iteration and goes to the next iteration.

See the syntax structure of “continue statement”

while ( expression 1)
{
if ( expression 2 )
{
continue ;
}
// Operation Statements
}

In this structure if expression 2 return TRUE , continue statement will execute and it will skip that iteration and wont execute the rest portions of code after the “continue” command, control will go to the next iteration in while loop with expression 1.

See the sample PHP program with “continue” statement

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 continue;
 }
 echo $a*$b." ";
}
echo "<br>";
}
?>

Output of this program will be
1 2 4 5
2 4 8 10
3 6 12 15

We can see that it skipped the third records in each loop.

We can use optional parameter with continue statement for specifying how many levels of loops ( not iteration ) should be skipped.

See the following continue syntax structure

while ( expression 1)
{
while ( expression 2 )
{
if ( expression 3 )
{
continue 2;
}
// Operation Statements
}
}

In this structure , if expression 3 becomes TRUE , continue statement will execute with parameter 2. So it will skip the current iteration in while loop with expression 2 also with current iteration with first while loop with expression 1. Next iteration will start from the first while loop.

See the sample php program with continue statement with parameter

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 continue 2;
 }
 echo $a*$b." ";
}
echo "<br>";
}
?>

Output of this program will be
1 2 2 4 3 6

Here we can see that when $b == 3 , control goes to the first while loop with next iteration. Due to the skipping of rest codes line break statement ( echo “<br>”; ) also wont execute.

Note : Difference between Continue and break statement is that

  • Break statement skip the current loop and control will come out from the current loop structure
  • Continue statement skip the current iteration only, control will goes to the next loop iteration

Do a check the programs with break statement in PHP

foreach array in PHP programming

foreach statement is used for making loop over array in loop. It will work only with arrays, and will make errors if we try it with other data types.

foreach statement can be used in two ways.. See the following two structures of foreach loop in PHP

foreach (array as $value)
{
// Operation statement
}

foreach (array as $key => $value)
{
// Operation statement
}

Difference between these two syntax are

  • In the first “foreach” statement , value of each array’s element will be assigned to the variable $value
  • In the second “foreach” statement, key of each array’s element will be assigned to the variable $key

See the sample PHP programs with foreach array loop structure

<?php
$arr = array(10, 20, 30, 40);
foreach ($arr as $value) {
    echo $value." ";
}
?>

Its output will be the values of each elements in array :
10 20 30 40

<?php
$arr = array(10, 20, 30, 40);
foreach ($arr as $key => $value) {
    echo $key." ";
}
?>

Its output will be the key vof each elements in array :
0 1 2 3

When “foreach statement” execute internal pointer will initialize to the first record of array and we don’t need to rest the arrays before the “foreach loop operation”

for in PHP loop programming

for statement is also used to make looping in PHP programming. Its structure is bit more complex than the other loop statements like “while”.

Its structure will look like

for ( expression 1 ; expression 2 ; expression 3 )
{
// Operation statements
}

In “for statement”

  • expression 1 will evaluate before the beginning of loop
  • expression 2 will evaluate before the beginning of each iteration
  • expression 3 will evaluate at the end of each iteration

See the sample program with for statement in PHP

<?php
for ($i=0 ; $i < 10 ; $i++)
{
echo $i." ";
}
?>

Its output will be
0 1 2 3 4 5 6 7 8 9

In this example the first expression ($i=0) will execute before the “for loop”.
Then it will execute the second expression ($i < 10) before each iteration.
At the end of each iteration third expression ($i++) will execute.

The expressions with “for statement” can be empty.
See this PHP program will give the same output like the previous example

<?php
$i=0;
for ( ; $i < 10 ; $i++)
{
echo $i." ";
}
?>

Its output will be :
0 1 2 3 4 5 6 7 8 9

And we can see that the first expression is empty in “for statement”.

PHP do while loop

“do while” loop is almost similar to “while” loop in PHP. The expression with “do while” statement will be executed only at the end of each iteration. So at the first iteration the code will execute before checking the expression. So in the do while loop the first iteration will surely executed .

This is the structure of a PHP do while loop

do
{
// Operation Statements
} while (expression);

See the sample PHP program with “do while” statement

<?php
$a = 0;
do
{
echo $a." ";
$a++;
}while ($a < 3);
?>

Its output will be
0 1 2

while loop in PHP

while statement is using for making a loop in PHP. The while loop will execute as long as the expression with while statement is TRUE. The loop will terminate when the expression return FALSE.

Its structure will be

while ( expression )
{
// Operation statements
}

We can use nested while structures also like
while ( expression 1 )
{
while ( expression 2 )
{
// Operation Statements
}
}

See the sample PHP programs with while statements

<?php
$x=0;
while ( $x < 3)
 {
  echo $x." ";
 $x = $x +1;
 }
?>

It will output the result
0 1 2

See the sample PHP programs with nested while statements

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 3)
{
 $b++;
 echo $a*$b." ";
}
echo "";
}
?>

It will output the result
1 2 3
2 4 6
3 6 9

Break in PHP

“break” statement is using for end the current execution of loop control structure.
Its applicable for for, foreach, while, do-while or switch control structures.

We can add optional parameter with break statement to specify how many control structure should end. By default its value will be 1. Means if we specify break; the it will treat as break 1;

If we specify break 2; it will ends the execution of two control structure .

Look the following structure
while (expression 1)
{
while (expression 2 )
{
break;
}
}
In this structure break statement will ends the execution of while loop with expression 2.

while (expression 1)
{
while (expression 2 )
{
break 2;
}
}
In this structure break statement will ends the execution of two while loop swith expression 1 and expression 2.

See the following sample PHP programs with break statement

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 break 1;
 }
 echo $a*$b." ";
}
echo "";
}
?>

Its output will be :
1 2
2 4
3 6
( when the value of $b = 3 break statement will ends the execution of while ( $b < 5) )

<?php
$a = 0;
while ( $a < 3)
{
$a++;
$b = 0;
while ( $b < 5)
{
 $b++;
 if ($b==3)
 {
 break 2;
 }
 echo $a*$b." ";
}
echo "";
}
?>

Its output will be :
1 2
( when the value of $b = 3 break statement will ends the execution of two while loops)

break statement is really useful when we want to terminate a loop control structure or a switch case in a particular condition. In some scenarios we need to search for something inside another thing in a loop. we can use break statement when we got the desired result and can eliminate the rest unwanted execution of loops.

Nested if statements in PHP

Nested if statements means an if block inside another if block. Shortly a control structure inside another control structure.

It structure will look like

if (expression 1 )
{
if (expression 2 )
{
// statements 1
}
else
{
// Statements 2
}
}
else
{
if ( expression 2)
{
// Statements 3
}
else
{
// Statements 4
}
}

Here we can see another if .. else structure inside the if block and else block. Like this we can add any number of nested if else statements.

Nested if statements will make the PHP codes more comples and lengthy. To avoid Nested statements we can opt for elseif statements

elseif statement in PHP

“elseif” statement is used to execute several blocks of if statements. If we want to execute a different statement if the original if statement return FALSE we can use elseif control statement.

Its structure will look like
if (expression 1 )
{
// operation statements 1
}
elseif (expression 2 )
{
// operation statements 2
}
elseif (expression 3 )
{
// operation statements 3
}
else
{
// operation statements 4
}

First the main if statement will execute and if it return FALSE , then “operation statements 1” will be avoided and control will go to the next elseif statement with expression 2. If “expression 2” return FALSE then “operation statements 2” will be avoided and control will go to the next elseif statement with expression 3. If “expression 3” return TRUE then “operation statements 3” will be executed otherwise control will go to else statement and “operation statements 4” will be executed.

See the sample PHP program with elseif statement

<?php
$x = 10;
if ( $x < 5 )
{
  echo " value is less than 5";
}
elseif ( $x < 10 )
{
echo "value is less than 10";
}
elseif ( $x < 15 )
{
echo "value is less than 15";
}
else
{
echo " all the above checking failed and else statement executed";
}

?>

Here the ouput will be
value is less than 15

First if statement will execute ( 10 < 5 ) and surely it will return FALSE.
Then control will go to the next elseif statement ( 10 < 10 ) , and of course it also return FALSE.
Then control will go to the next elseif statement ( 10 < 15 ) and it will return TRUE and the block of code inside that elseif statements will execute and it will display the result “value is less than 15”

Note : Space between elseif statement ( like “else if” ) wont parse by PHP compiler and will return error. Make sure there is no space in elseif in your PHP programs.

else statement in PHP

“else” statement is an extension with “if” statement in PHP programming. If need to execute a block of statements when if statement return FALSE, we use else statement combine with if statement.

Here is the structure of a PHP program with else .
if (expression )
{
// execute statements if expression returns TRUE
}
else
{
// execute statements if expression returns FALSE
}

See the sample PHP program with else statement

<?php
$x = 10;
if ( $x < 5 )
{
 echo " value is less than 5";
}
else
{
 echo " value is greater than or equal to 5";
}
?>

Output of this program will be :
value is greater than or equal to 5

In this case if statement will return FALSE ( because 10 < 5 statement will return FALSE ) , then else statement will execute and code inside the else block will execute and will display the result “value is greater than or equal to 5”.

Note : else statement will come only with if statement

if statement in PHP

“if” statement is one of the most important and most used construct in PHP programming.

Its syntax will look like

if  (expression)
{
// Operation statements
}

if statement’s return result will be a Boolean value like TRUE or FALSE.
If the expression with “if statement” returns TRUE, the Operation Statements will execute , otherwise it will ignore the statements.

See the sample PHP program with if statement

<?php
$x = 10;
if ( $x < 15 )
{
 echo " First Statement Executed ";
}
if ( $x > 15 )
{
 echo " Second Statement Executed ";
}
?>

Output of this program will be :
First Statement Executed

In this case ( $x < 15 ) will return TRUE because 10 is less than 15, so the code inside the if block will execute. ( $x > 15 ) will return FALSE and the code inside that if loop wont execute and wont display “Second Statement Executed” in result.

We can use the if statements in a nested structure with else , elseif statements.
See the next posts for learn about those.

Share
Share