Tags
PHP
Asked 1 years ago
25 Feb 2023
Views 434
steave ray

steave ray posted

how to use goto statement in php

goto statement in php
Mitul Dabhi

Mitul Dabhi
answered Feb 25 '23 00:00

In PHP, the goto statement is used to jump to another section of code within the same script . It allows the programmer to change the normal flow of execution in a program by jumping to a specified label.

Here is an example of how the goto statement can be used in PHP:


<?php

$count = 1;

start:
echo $count . "<br>";

$count++;

if ($count <= 10) {
    goto start;
}

?>

In this example, the goto statement is used to jump to the start label, which is located above the echo statement. The goto statement is used within a conditional statement to loop through and print out the values of $count until it reaches 10.

While the goto statement can be useful in some cases, it is generally considered a controversial feature of PHP and can make code more difficult to read and maintain. It is usually recommended to use structured programming constructs, such as loops and conditional statements, instead of the goto statement wherever possible.



Post Answer