Home > Backend Development > PHP Tutorial > Introduction and examples of php5.3 goto function_PHP tutorial

Introduction and examples of php5.3 goto function_PHP tutorial

WBOY
Release: 2016-07-13 10:35:24
Original
794 people have browsed it

The goto operator is a new function after PHP5.3+, which is used to jump to another location in the program; its usage is very simple: put the mark of the target location after goto, and mark the target location with the target name plus a colon. , as follows:

Copy code The code is as follows:

goto a;
echo 'Script Home';
a:
echo 'http://www.jb51.net';

But the target location of goto can only be the same file and scope [it cannot jump to a function or class method]. Of course, it can jump out of the loop, but it cannot jump into the loop:

Copy code The code is as follows:

for($i = 0; $i < 3; $i++)
{
echo $i . '
';
if($i == 1) goto end;
}
end :
echo 'End directly';

The goto operator can be used to jump to a specified location in the program. The target location can be marked with the target name followed by a colon. Goto in PHP has certain restrictions and can only jump within the same file and scope. This means that you cannot jump out of a function or class method, nor can you jump into another function. You also cannot jump into any loops or switch structures. Common usage is to break out of a loop or switch, which can replace multiple levels of break.

Copy code The code is as follows:

goto a;
echo 'Foo';

a:
echo 'Bar';
?>

The above routine will output: Bar

goto jump out of loop example

Copy code The code is as follows:

for($i =0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
?>

The above routine will output: j hit 17

The following writing is invalid

Copy code The code is as follows:

goto loop;
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
loop:
}
}
echo "$i = $i";
?>

The above routine will output:

Fatal error: 'goto' into loop or switch statement is disallowed in
script on line 2

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/744328.htmlTechArticleThe goto operator is a new function after PHP5.3+, which is used to jump to another location in the program; The usage is very simple: put the mark of the target position after goto, and mark the target position with the target name plus a colon...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template