Press "Enter" to skip to content

Break Statement in PHP

What is the Break Statement in PHP?

The break statement in PHP stops the execution of a for, while, foreach, do-while loop or a switch structure. When we use a break statement in an inner loop, then the break statement can terminate the inner loop only.

What is the Syntax of Break Statement for a Loop in PHP?

for(initialization;test condition;increment/decrement)
{
statement1;
statement2;
.
.
break;
}

outside statement1;

Now we are writing a program for using a break statement inside a for loop?

In this program, we are using a loop, and we are displaying the string “Gyanbriksh” here, the loop is terminating it’s execution when the value of loop variable $i is 5:

<html>
    <body>
        <?php
for($i=1;$i<=10;$i=$i+1)
{
    echo "Gyanbriksh"."<br>";
    if($i==5) //condition where loop stops the execution
        break;
}
        ?>
    </body>
</html>

Output

Gyanbriksh
Gyanbriksh
Gyanbriksh
Gyanbriksh
Gyanbriksh

This program is terminating its execution when the value of loop variable is 5. So here the loop is running for 5 times with displaying “Gyanbriksh”.

Here we are giving another example for checking a number, is prime or not.

We know that a prime number is a number which is divisible by 1 and itself only. We are using a variable $a for checking where it is prime or not, and we are using a loop which is running from 2 to <$a.

<html>
    <body>
        <?php
$a=6;
$count=0;
for($i=2;$i<$a;$i=$i+1)
{
    if($a%$i==0)
    {
        $count=$count+1; 
        break;
    }
}
if($count>0)
    echo "The number is not a prime number";
else
    echo "The number is a prime number";
        ?>
    </body>
</html>

Output

The number is not a prime number

Here we are giving a condition($a%$i==0). Here the value of $count is 1(because firstly 2 divides $a),$count is incrementing, and the loop is ending its execution using a break statement. So the string “The number is not a prime number” is displaying.

Here we are writing another example on break statement:

Here we are using an outer loop, and an inner loop within a function and the inner loop is terminating its execution based on a condition.

<html>
    <body>
        <?php
$sum=0;
function f1()
{
    for($i=1;$i<=2;$i=$i+1)
    {
        echo "Gyanbriksh"."<br>";
        for($j=1;$j<=2;$j=$j+1)
        {
            if($j>0) //condition for terminating inner loop
            {
                break;
                echo "well com"; 
            }
        }
    }
}
f1(); //function calling
        ?>
    </body>
</html>

Output

Gyanbriksh
Gyanbriksh

This program is using a break statement inside an inner loop, and the inner loop ends its execution when the value of the inner loop variable is greater than 0. Here outer loop is running for two times, and “Gyanbriksh” is displaying for two times and “well come” is not displaying for any time.

Now we are writing here a program for using a break statement in a switch structure.

In this program, we are taking a variable $a and a switch structure.

<html>
    <body>
        <?php
$a=1;
switch($a)
{
    case 1:
    echo "This is sunday";
    break;
    case 2:
    echo "This is Monday";
    break;
    case 3:
    echo "This is Tuesday";
    break;
    case 4:
    echo "This is Wedday";
    break;
    case 5:
    echo "This is Thursday";
    break;
    case 6:
    echo "This is Friday";
    break;
    case 7:
    echo "This is satday";
    break;
}
        ?>
    </body>
</html>

Output

This is Sunday

Here the value of $a is comparing with the case value, and first case value is matching with the value of $a. So “This is Sunday” is displaying and the break is using to prevent the code from running into the next case automatically inside the side.