PHP break

Summary: in this tutorial, you will learn how to use the PHP break statement to end the execution of the current for, do...while, whileAnd switch statements.

Introduction to the PHP break statement.

The break statement terminates the execution of the current for, do...while, while, or switch statement. This tutorial focuses on how to use the break statement with the loops.

Typically, you use the break statement with the if statement that specifies the condition for the terminating loop.

The break statement accepts an optional number that specifies the number of nested enclosing structures to be broken out of.

If you don’t specify the optional number, it defaults to 1. In this case, the break statement only terminates the immediate enclosing structure.

Using PHP break statement in a for loop

The following example illustrates how to use the break statement in a for loop:

<?php


for ($i = 0; $i < 10; $i++) {
	if ($i === 5) {
		break;
	}
	echo "$i\n";
}Code language: HTML, XML (xml)

Output:

0
1
2
3
4

The for statement would execute 10 times from 0 to 9. However, when the $i variable reaches 5, the break statement ends the loop immediately. The control is passed to the statement after the for statement.

Using PHP break statement in a do…while loop

The following example illustrates how to use the break statement inside a do...while loop:

<?php

$j = 0;
do {
	if ($j === 5) {
		break;
	}
	echo "$j\n";
	$j++;
} while ($j <= 10);Code language: HTML, XML (xml)

Output:

0
1
2
3
4

In this example, the do-while loop executes only five iterations. The variable $j is increased by one in each iteration. When the $j variable reaches 5, the break statement terminates the loop immediately.

Using PHP break statement in a while loop

The following example shows how to use the break statement in a while loop:

$k = 0;
while ($k <= 10) {
	if ($k === 5) {
		break;
	}
	echo "$k\n";
	$k++;
}Code language: PHP (php)

Output:

0
1
2
3
4

This example also displays five numbers from 0 to 4. In each iteration, the variable $k is increased by one. The break statement terminates the loop immediately when the $k variable reaches 5.

Using break statement to jump out of a nested loop

The following example illustrates how to use the break statement to break of out a nested loop:

$i = 0;
$j = 0;
for ($i = 0; $i < 5; $i++) {
	for ($j = 0; $j < 3; $j++) {
		if ($i === 3) {
			break 2;
		}
		echo "($i, $j)\n";
	}
}Code language: PHP (php)

Output:

(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)

In this example, when the variable $i reaches 3, the break statement terminates the inner and outer loops immediately.

By default, the break statement only ends the enclosing loop. But in this example, we use the number 2 in the break statement; therefore, it terminates both inner and outer loops.

If you remove the number 2, you will see a different output:

<?php

$i = 0;
$j = 0;
for ($i = 0; $i < 5; $i++) {
	for ($j = 0; $j < 3; $j++) {
		if ($i === 3) {
			break;
		}
		echo "($i, $j)\n";
	}
}Code language: HTML, XML (xml)

Output:

(0, 0)
(0, 1)
(0, 2)
(1, 0)
(1, 1)
(1, 2)
(2, 0)
(2, 1)
(2, 2)
(4, 0)
(4, 1)
(4, 2)

In this example, the break statement ends the inner loop when $i is 3.

Summary

  • Use the break statement to immediately terminate the execution of the current for, do...while, and while loops.
Did you find this tutorial useful?