PHP

PHP Menu

PHP

Break/Continue - PHP Basics

The break statement can be used to exit out of a loop.

This example below exits the loop when $i is equal to 5:

<?php
for ($i = 0; $i <= 10; $i++) { if ($i == 5) { break; } echo "The number is: $i <br>"; }

PHP continue

The continue statement skips one iteration of the loop, if a specified condition occurs, and continues with the next iteration in the loop.

The example below skips printing the value of 5:

<?php
for ($i = 0; $i < 10; $i++) { if ($i == 5) { continue; } echo " $i <br>"; }

Exercise

Using the for loop print the numbers from 5 to 1 (decreasing order) but exits when the number reaches 3.

<?php
$var = 123;
<?php
for ($i = 5; $i >= 1; $i--) { if ($i == 2) { break; } echo $i . "<br>"; }
{ "test_output_contains":[ { "expected":"5", "error_message":"Sorry. Please try again." }, { "expected":"4", "error_message":"Sorry. Please try again." }, { "expected":"3", "error_message":"Sorry. Please try again." } ], "success_message":"Good job!", "error_message":"Please read the instructions again." }

Introduction

PHP Basics

PHP Advance

PHP OOP

PHP Functions and Methods