ADVERTISEMENT
ADVERTISEMENT

Jumping Statements in PHP

A jumping statement in PHP is used to alter the normal flow of execution by transferring control to different parts of the code. It allows you to break out of loops, skip iterations, or exit functions based on conditions. Common jumping statements in PHP are break, continue, return, and goto.

PHP has four main jumping statements:

  • break – Exits from a loop or switch statement.
  • continue – Skips the current iteration of a loop and moves to the next one.
  • return – Exits from a function and optionally returns a value.
  • goto – The goto statement in PHP is used to jump to a specific labeled point in the code.

1. break Statement

The break statement is used to terminate the execution of a loop or switch statement. When break is encountered, the program immediately exits the loop or switch and continues with the next line of code outside the loop or switch.

Syntax:

break;

Example:

<?php
for ($i = 1; $i <= 10; $i++)
 {
    if ($i == 5) {
        break; // Exit the loop when $i equals 5
    }
    echo "Number: $i <br>";
}
?>

In this example, the loop stops when $i equals 5 due to the break statement.

2. continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration. If continue is inside a loop, it ignores the remaining code for that iteration and proceeds with the next cycle.

Syntax:

continue;

Example:

<?php
for ($i = 1; $i <= 5; $i++) 
{
    if ($i == 3) 
    {
        continue; // Skip the iteration when $i equals 3
    }
    echo "Number: $i <br>";
}
?>

Here, when $i equals 3, the continue statement skips the iteration, so 3 is not printed.

3. return Statement

The return statement is used to exit from a function and optionally send a value back to the function's caller. Once return is executed, the function terminates, and control is passed back to the part of the program that called the function.

Syntax:

return [value];

Example:

<?php
function add($a, $b) 
{
    return $a + $b; // Return the sum of $a and $b
}

$result = add(5, 10);
echo "Sum: $result"; // Outputs: Sum: 15
?>

In this example, the return statement exits the function and provides the sum of $a and $b to the caller.

4. goto Statement

The goto statement in PHP is used to jump to a specific point in the code, marked by a label. It is not commonly recommended to use due to the potential for creating unreadable and difficult-to-maintain code. It’s considered a "jump" statement like break, continue, and return, but it allows for unconditional jumping to any point in the code.

Syntax:

goto label;

...

label:

// Code to execute

Example:

<?php

echo "Start
";

goto skip; // Jump to the label

echo "This won't be printed
";

skip:
echo "This will be printed after the jump
";

?>

Output:

Start

This will be printed after the jump

In the example above, the goto skip; statement causes the code to jump to the skip: label, skipping the line that would have printed "This won't be printed".


ADVERTISEMENT

ADVERTISEMENT