PHP Goto

In this tutorial we will study about goto statement, goto statement helps us to move from one section to another section in a file, in PHP there are few restrictions are present for goto statement which is discussed in the current tutorial. Examples in this tutorial will make it more clear.

 
 

Share on Google+Share on Google+

PHP Goto

Ads

In this tutorial we will study about goto statement, goto statement helps us to move from one section to another section in a file, in PHP there are few restrictions are present for goto statement which is discussed in the current tutorial. Examples in this tutorial will make it more clear.

Goto Statement:

The goto operator is used to jump to another line or section in the program. To move to another section we need to specify a name after goto statement and to declare a target we need to give a specific name followed by a colon. In PHP there are few constraints in goto statements, like we can not move from one file/method to another file/method or neither you can move inside a loop/switch structure using goto statement. You may jump out of these constructs using goto statement.

Example:

<?php

echo "First Statement <br/>";

goto rd;

echo "Second statement <br/>";

rd:

echo "Third statement";

?>

Output:

First Statement
Third statement

Example:

<?php

goto mid;

$a=12;

while($a>0){

$a--;

mid:

echo "Inside the goto statement";

}

?>

Output:


Fatal error: 'goto' into loop or switch statement is disallowed in C:\xampp\htdocs\PHP-AJAX\variable.php on line 2

Example:

<?php

$a=12;

while($a>0){

$a--;

if($a==6)

{

goto mid;

}

else

{

echo "<br/>$a";

}

}

mid:

echo "<br/>Inside the goto statement";

?>

Output:

11
10
9
8
7
Inside the goto statement

Advertisements

Ads

Posted on: March 29, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles