Break and continue keywords php

Break and continue keywords php

In PHP program development, the break and continue keywords are used to control and manage the default flow behavior of for, while, and foreach loops, conditional statements, and switch statements. These break and continue keywords are used to break or continue the default execution of for, while, do-while, foreach loops, and switch statements.

Break and continue keywords php

break keyword in PHP.

In PHP program development, the break keyword is used to break the default flow of a program loop or switch statement or to exit the active loop. When the break keyword is applied to a program, the running program loop or switch statement exits the immediate break or loop condition. By default, program control moves to the next statement after the current loop or switch statement.

Use of the break keyword in a loop.

The break keyword, the default behavior of a running program, will immediately stop the execution of a program loop, regardless of whether the active loop condition is true or false.

Example of the break keyword in a for loop.

<?php

for ($p = 0; $p <= 11; $p++) {

if ($p == 8) {

break; // Here the break keyword is used to exit the loop when it reaches $p is == 8

}

echo $p . ” “; // Result – 0 1 2 3 4 5 6 7

}

?>

Here’s an example of the break keyword.

  • In this program, the for loop iterates and prints numeric values ​​from 0 to 11. However, as soon as $p equals 8, the break statement is executed, causing the loop to terminate immediately.
  • The result here will be 0 1 2 3 4 5 6 7, because the for loop stops when $p == 8.

Example of the break keyword in a switch statement.

<?php

$weekday = 1;

switch ($weekday) {

case 1:

echo “Monday”; // Result – Monday

break;

case 2:

echo “Tuesday”;

break;

case 3:

echo “Wednesday”;

break;

case 4:

echo “Thursday”;

break;

default:

echo “unknown weekday”;

}

?>

In this break keyword example.

  • the switch statement in this program checks all multiple case values ​​of $weekday.
  • When the case value is 1, the result displays “monday,” and then the break statement exits the switch statement block.

The continue keyword in PHP.

In PHP program development, the continue keyword is used to stop the current iteration of a loop, move to the next iteration, and continue printing the remaining loop statements. When the continue keyword is used after a break in the program, the loop stops the current condition and continues printing the next loop statement in the iteration until the loop condition automatically terminates.

Use of the continue keyword in a loop.

Remember, the continue statement does not terminate the loop; instead, it prints the next iteration statement according to the loop condition, leaving the remaining program source code intact.

Example of the continue keyword in a for loop.

<?php

for ($p = 0; $p <= 11; $p++) {

if ($p == 8) {

continue; // Skip the iteration when $p is equal to 8

}

echo $p . ” “; // Result – 0 1 2 3 4 5 6 7 9 10 11

}

?>

In this example,

  • The for loop iterates from 0 to 11 in the program.
  • When the value of $p reaches or equals 8, the continue statement is executed, skipping the code remaining in that repetition statement in the loop, and the loop continues printing the next iteration ($p = 9).

Example of the continue keyword in a while loop.

<?php

$p = 0;

while ($p <= 11) {

$p++;

if ($p == 7) {

continue; // here it skips the iteration when the while loop is equal to $p is 7

}

echo $p . ” “; // Result – 1 2 3 4 5 6 8 9 10 11 12

}

?>

Here in the while continue example.

  • Here in the while loop, statements 1 through 10 are repeated.
  • When $p reaches or equals 7, the continue statement is executed, which skips the while loop where $p = 7 and prints the next remaining statement.

Example of a continue with an else condition in a foreach loop.

<?php

$printers = [“hp printer”, “canon printer”, “brother printer”, “epson printer”];

foreach ($printers as $printer) {

if ($printer == “brother printer”) {

continue; // Skip this iteration when the printer reaches brother printer

}

echo $printer . ” “; // Result – hp printer canon printer epson printer

}

?>

In this program example.

  • In this program, the foreach loop iterates over the $printers array element.
  • When $printer is “brother printer”, the continue statement is executed, skipping that iteration and reading and printing the next printer statement.

Using the break and continue keywords together in PHP.

In a PHP program, you can manage this by using both the break and continue keywords together in a single loop, where the loop executes under multiple conditions.

Example of the break and continue keywords together in a for loop.

<?php

for ($p = 0; $p <= 11; $p++) {

if ($p == 5) {

continue; // here when p==8 it will skip 5

}

if ($p == 9) {

break; // here it will break or exit the loop when it reaches $p==9

}

echo $p . ” “; // Result – 0 1 2 3 4 6 7 8

}

?>

In this program example, when $p reaches or equals 5, the continue statement is executed, skipping that loop iteration.

When $p reaches or equals 9, the program executes a break statement, exiting the loop.

Key difference between break and continue statement

KeywordBreak and continue statement PurposeUsage
Break statementBreak statement used to immediate Exits the loop or switch statement immediatelyBreak keyword helps us to Stops the current loop iteration or switch block statement
Continue statementContinue statement used to Skips the current iteration of a loop and continues to the next iteration in the programContinue keyword used to Skips to the next iteration of the loop, without executing the rest of the current iteration’s program code

A summary of the break and continue keywords in PHP program development.

  • In PHP program development, the break keyword is commonly used to exit a loop when a condition is met or to print a switch statement.
  • In PHP program development, the continue keyword is used to print the remaining statements in a loop, excluding the current iteration. The continue statement is often used when a condition is met and the program does not want the remaining code in the loop to be executed for that iteration.
  • In PHP program development, both the break and continue statement keywords are essential for managing the flow of loops and controlling conditional logic in PHP programs.

Leave a Reply