php—goto statement

伊谢尔伦
Release: 2016-11-24 09:08:13
Original
1405 people have browsed it

The

goto operator can be used to jump to another location in the program. The target position can be marked with the target name plus a colon, and the jump instruction is the mark of the target position after goto. goto in PHP has certain restrictions. The target location can only be in the same file and scope, which means that it cannot jump out of a function or class method, nor can it jump into another function. It also cannot jump into any loop or switch structure. You can jump out of a loop or switch. The usual usage is to use goto instead of multi-layer break.

Example #1 goto example

<?php
goto a;
echo &#39;Foo&#39;;
 
a:
echo &#39;Bar&#39;;
?>
Copy after login

The above routine will output:

Bar

Example #2 goto jump out of loop example

<?php
for($i=0,$j=50; $i<100; $i++) {
  while($j--) {
    if($j==17) goto end; 
  }  
}
echo "i = $i";
end:
echo &#39;j hit 17&#39;;
?>
Copy after login

The above routine will output:

j hit 17

Example #3 below Invalid writing method

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

The above routine will output:

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

Note: The

goto operator is only valid in PHP 5.3 and above. .


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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!