- Similar to C-language, PHP also provides goto statement to transfer control unconditionally to other part of the program.
- Although use of goto statement is not advisable but sometime it is desirable to use go to statement.
- It has the following form:
- goto statement requires a label to determine where to transfer the control.
- A label must end with colon (:)
- We can use any valid name as a label similar to variable name.
- When compiler encounters goto statement with a label name then, it transfers the control to the location where label has been defined in the program.
- When we use goto statement either some statement are execute repeatedly or skipped.
- When goto statement is placed after the label, control jumps backward direction and some statement are repeated. Such type of go to jump is called backward jump.
- When goto statement is placed before the label in the program, control transfers to the label and some statements are skipped. Such type of jump is called forward jump.
- In highly structure programming language such as c, it is not advisable to use go to statement.
- We should avoid using go to statement as far as possible because it affects performance of the program.
- The following program demonstrates the use of goto statement.
/* PHP program to calculate sum using goto statement.
$x = 10;
$y = 20 ;
if( x > 0 && y > = 0)
goto input ;
echo "</br>."This statement will be skipped because of goto" ;
$sum = $x + $y ;
echo "\n Answer is $sum" ;