goto


php128 apg

Can be used to jump to another section in the program.

Available since PHP 5.3.0.


The goto operator is used, at run-time, to jump to an specified point of the program.

This point is specified by a label followed by a semicolon, just after to the operator goto.

The targeted label has the same name, however, followed by a colon.



<?php

goto target;

. . . . . .

target:
       
statement


where
,

target the target point specified by a label

statement 
code to be executed
             
?>

  1 EXERCISE   

<?php

for ($i1 0$i1 <= 10$i1++)
{
    
     if (
$i1 == 5)
     {
     goto 
point5;
     }
     elseif (
$i1 == 6)
     {
     goto 
poincr6;
     }
     else
     {
     goto 
end;
     }
    
}    
     

point5:
echo 
'Targeted to point5<br>';

poincr6:
echo 
'Targeted to poincr6<br>';

end:
echo 
'Targeted to end<br>';

?>

 RESULT   

Targeted to end

  2 EXERCISE   

<?php

$i2 
mt_rand(17);
    
     if (
$i2 == 5)
     {
     goto 
point5;
     }
     elseif (
$i2 == 6)
     {
     goto 
poincr6;
     }
     else
     {
     goto 
end;
     }
     

point5:
echo 
'( ' $i2 ' ) Targeted to point5<br>';
exit;

poincr6:
echo 
'( ' $i2 ' ) Targeted to poincr6<br>';
exit;

end:
echo 
'( ' $i2 ' ) Targeted to end<br>';
exit;

?>