Home >Backend Development >PHP Tutorial >Detailed introduction to goto operator in php

Detailed introduction to goto operator in php

黄舟
黄舟Original
2017-06-25 13:31:221996browse

php gotoOperator

can be used to jump to another location in the program. The position can be marked with the target name plus a colon, and the jump instruction is followed by goto followed by the target position mark

Some restrictions on the use of the goto operator

The target position can only Located in the same file and scope

Cannot "jump out" of a function and a method of a class

Cannot "jump" into another function

Cannot "jump" into any loop or switch structure

You can "jump out" of a loop or switch. The general usage is to replace multi-layer break

Simple practical case

goto  target;
echo  'Hi world' ;
target :
echo  'hello world' ;

Result

hello world
$i = 0;
$j = 50 ; 
for( $i < 100 ;  $i ++) {
while( $j --) {
if( $j == 17 ) 
goto  end ; 
}  
}
echo  "i =  $i " ;
end :
echo  &#39;j hit 17&#39; ;

Result

j hit 17

The above is the detailed content of Detailed introduction to goto operator in php. For more information, please follow other related articles on the PHP Chinese website!

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