Home  >  Article  >  Backend Development  >  Introduction to PHP goto statement and usage examples_PHP tutorial

Introduction to PHP goto statement and usage examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:36:14745browse

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.

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, as follows:

Copy code Code As follows:
goto a;
echo 'Foo';//This sentence is skipped

a:
echo 'Bar';

//The output result of the above example is: Bar;

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

//The output result of the above example is: j hit 17
?>


Note:
The goto operator is only available in PHP 5.3 and above.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/740213.htmlTechArticleThe 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, it can only be used in the same file...
Statement:
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