How is the goto statement used in PHP?

In PHP, the goto statement can be used to unconditionally transfer program control flow to a specified label position. It can be used within loops, switch statements, or functions to directly jump to a designated label, even within nested structures.

In the examples below, we use the goto statement to jump to the label position end.

<?php
$i = 0;

start:
$i++;
echo $i . "<br>";

if ($i < 5) {
    goto start;
}

end:
echo "End of loop";
?>

In the above example, the statement “goto start;” will transfer program control to the label ‘start’ until the value of $i is less than 5, then it will jump to the label ‘end’. It should be noted that using the ‘goto’ statement may make the program difficult to maintain and understand, so it should be used cautiously.

广告
Closing in 10 seconds
bannerAds