codescracker


php

PHP break and continue Keyword



« Previous Tutorial Next Tutorial »


Here you will learn all about break and continue keyword in PHP with examples.

PHP break Keyword

PHP break keyword is used to terminate the execution of the loop immediately. Here is an example of break keyword in PHP:

<html>
<head>
	<title>PHP break Keyword Example</title>
</head>
<body>
<?php
	$i = 10;
	while($i > 0)
	{
		$i--;
		echo $i." ";
		if($i == 5)
		break;
	}
?>
</body>
</html>

In the above PHP script, the loop will conditnue executing until (before) i reached to 5, and then the condition of the if statement becomes true, and break statement executes to terminate the loop immediately. Here is the output of the above PHP script:

PHP break keyword

Let's take another example on break keyword in PHP.

<html>
<head>
	<title>PHP break Keyword Example</title>
</head>
<body>
<?php
	$tutorial = ['Java', 'C', 'C++', 'JavaScript', 'PHP', 'Nothing'];
	foreach($tutorial as $value)
	{
		if($value == 'Nothing')
		{
			break;
		}
		echo "I just love to read ".$value." on CodesCracker<br/>";
	}
?>
</body>
</html>

Here is the sample output produced by the above break keyword in PHP.

break keyword in php

PHP continue keyword

PHP continue keyword is used to skip the remaining part of the code and program control goes to the condition-checker part of the loop. Here is an example, uses continue keyword in PHP:

<html>
<head>
	<title>PHP continue Keyword Example</title>
</head>
<body>
<?php
	$i = 10;
	while($i > 0)
	{
		$i--;
		if($i == 5)
		continue;
		echo $i." ";
	}
?>
</body>
</html>

Here is the output produced by the above PHP script:

PHP continue keyword

Let's take another example on continue keyword in PHP.

<html>
<head>
	<title>PHP continue Keyword Example</title>
</head>
<body>
<?php
	$tutorial = ['Java', 'C', 'C++', 'Nothing', 'JavaScript', 'PHP'];
	foreach($tutorial as $value)
	{
		if($value == 'Nothing')
		{
			continue;
		}
		echo "I just love to read ".$value." on CodesCracker<br/>";
	}
?>
</body>
</html>

Here is the sample output produced by the above continue keyword example code in PHP.

continue keyword php

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test