php goto statement

Goto statement is used to you can suddenly jump to a specific location outside of a looping or conditional construct.

Goto Syntax :

<?php
for (loop)
{
   if (condition)
    {
    --------
    --------
    --------
    goto;
   }
}
?>

Goto Example:


<?php for ($count = 0; $count < 10; $count++)
{
$randomNumber = rand(1,50);
if ($randomNumber < 10)
goto less;
else
echo "Number greater than 10: $randomNumber<br />";
}

less:
echo "Number less than 10: $randomNumber<br />";
?>

Out put:

Number greater than 10: 22
Number greater than 10: 21
Number greater than 10: 35
Number less than 10: 8

In the above example we have used PHP random function to generate the number . which random number if less then 10 the GOTO statement call to less part otherwise loop will be continue.





Content