Introduction
PHP provides control-flow statements that change how a program executes. The break statement exits a loop or switch block, while continue skips the remaining statements in the current loop iteration and moves to the next one. The goto statement transfers execution to a labeled statement within the same file and scope. In this chapter, you will practise PHP Break, Continue and Goto Statements Practice Questions with Solutions to help you understand the concepts.
Question 1: Stop a Loop When the Number Reaches 5 Using Break
Problem Statement
Write a PHP program that prints numbers from 1 to 10. Use the break statement to stop the loop when the number reaches 5.
PHP Solution
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break;
}
echo $i . "\n";
}
?>
Sample Output
1
2
3
4
Explanation
The for loop starts at 1 and increases the value of $i by 1 during each iteration.
When $i becomes 5, the if condition becomes true. The break statement immediately terminates the loop, so 5 and the remaining numbers are not printed.
Concepts Covered
forloopifstatementbreakstatement- Loop termination
- Increment operator
Question 2: Skip Even Numbers Using Continue
Problem Statement
Write a PHP program that prints numbers from 1 to 10 but skips all even numbers using the continue statement.
PHP Solution
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
continue;
}
echo $i . "\n";
}
?>
Sample Output
1
3
5
7
9
Explanation
The modulo operator (%) returns the remainder after division. When a number is divided by 2 and the remainder is 0, it is even.
The continue statement skips the echo statement for even numbers. The loop then proceeds to the next iteration.
Concepts Covered
continuestatementforloop- Modulo operator
- Even and odd numbers
- Conditional execution
Question 3: Find the First Number Divisible by 7 Using Break
Problem Statement
Write a PHP program that checks numbers from 1 to 50 and displays the first number divisible by both 7 and 5. Stop the loop when the number is found.
PHP Solution
<?php
for ($i = 1; $i <= 50; $i++) {
if ($i % 7 == 0 && $i % 5 == 0) {
echo "First matching number: " . $i;
break;
}
}
?>
Sample Output
First matching number: 35
Explanation
The loop checks each number from 1 to 50.
The condition $i % 7 == 0 && $i % 5 == 0 checks whether the number is divisible by both 7 and 5 without a remainder.
The first number satisfying both conditions is 35. The program displays it and uses break to stop checking further numbers.
Concepts Covered
breakstatement- Modulo operator
- Logical AND (
&&) - Divisibility checks
- Loop termination
Question 4: Skip Employees with Zero Salary Using Continue
Problem Statement
A company stores employee names and their salaries in an associative array. Write a PHP program that skips employees whose salary is zero and displays the details of the remaining employees.
PHP Solution
<?php
$employees = [
"Amit" => 30000,
"Priya" => 0,
"Rahul" => 45000,
"Neha" => 0,
"Karan" => 38000
];
foreach ($employees as $name => $salary) {
if ($salary == 0) {
continue;
}
echo "Employee: " . $name . ", Salary: ₹" . $salary . "\n";
}
?>
Sample Output
Employee: Amit, Salary: ₹30000
Employee: Rahul, Salary: ₹45000
Employee: Karan, Salary: ₹38000
Explanation
The foreach loop processes each employee and their salary.
When the salary is zero, the continue statement skips the remaining statements for that employee. Employees with non-zero salaries are displayed.
This technique is useful when processing company records and ignoring entries that do not meet specific requirements.
Concepts Covered
foreachloop- Associative arrays
continuestatement- Employee data processing
- Conditional filtering
Question 5: Stop Processing Student Marks When an Invalid Value Appears
Problem Statement
A teacher stores student marks in an array. Write a PHP program that displays each mark but stops processing when it encounters a mark greater than 100 or less than 0.
PHP Solution
<?php
$marks = [75, 82, 68, 95, 110, 88];
foreach ($marks as $mark) {
if ($mark < 0 || $mark > 100) {
echo "Invalid mark found: " . $mark;
break;
}
echo "Mark: " . $mark . "\n";
}
?>
Sample Output
Mark: 75
Mark: 82
Mark: 68
Mark: 95
Invalid mark found: 110
Explanation
The loop processes each mark in the array.
Before displaying a mark, the if statement checks whether it is outside the valid range of 0 to 100. When the value 110 is found, the program displays an error message and stops the loop using break.
The final value, 88, is never processed because the loop has already terminated.
Concepts Covered
foreachloopbreakstatement- Arrays
- Logical OR (
||) - Input validation
Question 6: Skip Products That Are Out of Stock
Problem Statement
An online store maintains a list of products and their available stock quantities. Write a PHP program that skips products with zero stock and displays only the products that are available.
PHP Solution
<?php
$products = [
"Keyboard" => 12,
"Mouse" => 0,
"Monitor" => 5,
"Printer" => 0,
"Headphones" => 8
];
foreach ($products as $product => $stock) {
if ($stock <= 0) {
continue;
}
echo "Product: " . $product . ", Stock: " . $stock . "\n";
}
?>
Sample Output
Product: Keyboard, Stock: 12
Product: Monitor, Stock: 5
Product: Headphones, Stock: 8
Explanation
The program uses a foreach loop to examine each product’s stock quantity.
If the stock is zero or less, continue skips the current product. Otherwise, the product name and stock quantity are displayed.
This approach can help when generating reports of products currently available for sale.
Concepts Covered
foreachloopcontinuestatement- Associative arrays
- Inventory management
- Conditional filtering
Question 7: Exit a Switch Statement Using Break
Problem Statement
Write a PHP program that uses a switch statement to display a message based on a selected menu option. Use break to prevent PHP from executing the following cases.
PHP Solution
<?php
$choice = 2;
switch ($choice) {
case 1:
echo "Add a new record";
break;
case 2:
echo "View existing records";
break;
case 3:
echo "Delete a record";
break;
default:
echo "Invalid menu option";
}
?>
Sample Output
View existing records
Explanation
The switch statement checks the value of $choice.
Since the value is 2, PHP executes case 2 and displays "View existing records". The break statement exits the switch block, preventing the following cases from executing.
Concepts Covered
switchstatementcaseblocksbreakstatement- Menu selection
- Program control flow
Question 8: Use Break 2 to Exit Nested Loops
Problem Statement
Write a PHP program that uses two nested loops to display pairs of numbers. Stop both loops when the outer number is 2 and the inner number is 3.
PHP Solution
<?php
for ($i = 1; $i <= 3; $i++) {
for ($j = 1; $j <= 5; $j++) {
if ($i == 2 && $j == 3) {
break 2;
}
echo "(" . $i . ", " . $j . ")\n";
}
}
?>
Sample Output
(1, 1)
(1, 2)
(1, 3)
(1, 4)
(1, 5)
(2, 1)
(2, 2)
Explanation
The program contains an outer loop controlled by $i and an inner loop controlled by $j.
When $i is 2 and $j is 3, the condition becomes true. The statement break 2 exits two levels of nested loops, so neither loop continues.
A plain break would exit only the inner loop.
Concepts Covered
- Nested loops
break 2- Logical AND (
&&) - Multiple loop termination
- Control-flow levels
Question 9: Use Goto to Jump to a Label
Problem Statement
Write a PHP program that displays a starting message, jumps to a labeled statement using goto, and then displays a final message.
PHP Solution
<?php
echo "Program started\n";
goto finish;
echo "This message will be skipped\n";
finish:
echo "Program finished";
?>
Sample Output
Program started
Program finished
Explanation
The goto finish; statement transfers program execution to the label named finish.
The statement between goto and the label is skipped, so "This message will be skipped" is not displayed.
The label is written as a name followed by a colon. Although goto is available in PHP, excessive use can make programs harder to understand and maintain.
Concepts Covered
gotostatement- Labels
- Jumping within program flow
- Skipping statements
- Code readability
Question 10: Skip Negative Numbers and Stop at Zero
Problem Statement
Write a PHP program that processes a list of numbers. Skip negative numbers using continue, display positive numbers, and stop the loop when the number zero is encountered using break.
PHP Solution
<?php
$numbers = [10, -5, 20, -3, 15, 0, 25, 30];
foreach ($numbers as $number) {
if ($number == 0) {
echo "Zero found. Stopping the loop.";
break;
}
if ($number < 0) {
continue;
}
echo $number . "\n";
}
?>
Sample Output
10
20
15
Zero found. Stopping the loop.
Explanation
This program demonstrates break and continue together.
- If the number is zero,
breakstops the entire loop. - If the number is negative,
continueskips the current iteration. - If the number is positive, PHP displays it.
Once zero is encountered, the loop stops, so the numbers after zero are not processed.
Concepts Covered
breakstatementcontinuestatementforeachloop- Conditional statements
- Multiple control-flow statements
Key Takeaways
- The
breakstatement terminates the current loop orswitchblock. - The
continuestatement skips the remaining statements in the current loop iteration. - The
gotostatement transfers execution to a labeled statement within the same file and scope. break 2can exit two levels of nested loops.- You can use
breakandcontinuewithfor,while,do-while, andforeachloops. - In a
switchstatement,breakprevents execution from continuing into subsequent cases. continueis useful for skipping invalid records or entries that do not meet a condition.gotoin PHP should be used sparingly because it can make program flow harder to follow.- Choose the appropriate control-flow statement based on whether you need to stop a loop, skip one iteration, or jump to a label.
Frequently Asked Questions (FAQs)
1. What is the break statement in PHP?
The break statement immediately exits the current loop or switch block. It is useful when a condition is met and no further iterations or cases need to be processed.
2. What is the continue statement in PHP?
The continue statement skips the remaining statements in the current loop iteration and proceeds to the next iteration.
3. What is the difference between break and continue?
break stops the loop completely, while continue skips only the current iteration and allows the loop to continue.
4. Can we use break inside a switch statement?
Yes. break is commonly used inside a switch statement to exit the matching case block and prevent execution from continuing into later cases.
5. What does break 2 mean in PHP?
break 2 exits two levels of nested loops or control structures. It is useful when a condition inside an inner loop must stop both the inner and outer loops.
6. What is the goto statement in PHP?
The goto statement transfers program execution to a labeled statement within the same file and scope, subject to PHP’s restrictions on jumping into certain structures.
7. Can goto jump into a loop or switch statement?
PHP restricts jumping into certain structures, including loops and switch statements. Use goto only where PHP permits the jump, and prefer structured control flow whenever possible.
8. Can continue be used inside a foreach loop?
Yes. continue works inside a foreach loop and skips the remaining statements for the current element before processing the next element.
9. Should beginners use goto in PHP?
Beginners should understand how goto works but generally prefer loops, conditions, functions, and other structured control-flow tools. These usually make code easier to read and maintain.
10. Can break and continue be used together in the same loop?
Yes. A loop can use break to stop processing completely and continue to skip individual iterations, depending on the conditions.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
