← Back to Chapters

PHP Continue & Break Statement

? PHP Continue & Break Statement

? Quick Overview

The continue and break statements in PHP allow developers to control loop execution. break exits the loop immediately, while continue skips the current iteration and proceeds with the next cycle.

? Key Concepts

  • break terminates loop execution completely
  • continue skips current iteration only
  • Commonly used inside for, while, and foreach loops

? Loop Simulator

Target Condition: When Number == 3

 

 

⚙️ Syntax / Theory

? View Code Example
// Terminates the loop immediately
break;
// Skips the current iteration and continues
continue;

? Code Example (break)

? View Code Example
// Loop stops when value reaches 3
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo "Number: $i
";
}
?>

? Live Output / Explanation

The loop prints numbers 1 and 2 only. When $i becomes 3, the break statement stops the loop entirely.

? Code Example (continue)

? View Code Example
// Skips printing when value equals 3
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo "Number: $i
";
}
?>

? Live Output / Explanation

The loop skips number 3 but continues execution, resulting in output: 1, 2, 4, and 5.

? Interactive Understanding

Think of break as an emergency exit ? from a loop, while continue is like skipping one step ⏭️ and moving ahead.

? Use Cases

  • Stopping data processing when a condition is met
  • Ignoring invalid data during iteration
  • Improving performance by exiting loops early

✅ Tips & Best Practices

  • Use break to stop loops as soon as the goal is achieved
  • Use continue to skip unnecessary processing
  • Keep loop logic readable and simple

? Try It Yourself

  • Create break_continue.php
  • Print numbers 1–10 but skip 5 using continue
  • Stop loop execution when number reaches 7 using break