Source of: /manual/en/control-structures.goto.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.control-structures.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'control-structures.goto.php',
1 => 'goto',
),
'up' =>
array (
0 => 'language.control-structures.php',
1 => 'Control Structures',
),
'prev' =>
array (
0 => 'function.include-once.php',
1 => 'include_once',
),
'next' =>
array (
0 => 'language.functions.php',
1 => 'Functions',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="control-structures.goto" class="sect1">
<h2 class="title"><i>goto</i></h2>
<p class="para">
The <i>goto</i> operator can be used to jump to another
section in the program. The target point is specified by a label
followed by a colon, and the instruction is given as
<i>goto</i> followed by the desired target label. This
is not a full unrestricted <i>goto</i>. The target
label must be within the same file and context, meaning that you cannot jump
out of a function or method, nor can you jump into one. You also
cannot jump into any sort of loop or switch structure. You may jump
out of these, and a common use is to use a <i>goto</i>
in place of a multi-level <i>break</i>.
</p>
<p class="para">
</p><div class="example">
<p><b>Example #1 <i>goto</i> example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">goto </span><span style="color: #0000BB">a</span><span style="color: #007700">;<br />echo </span><span style="color: #DD0000">'Foo'</span><span style="color: #007700">;<br /> <br /></span><span style="color: #0000BB">a</span><span style="color: #007700">:<br />echo </span><span style="color: #DD0000">'Bar'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Bar
</pre></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #2 <i>goto</i> loop example</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">for(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$j</span><span style="color: #007700">=</span><span style="color: #0000BB">50</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700"><</span><span style="color: #0000BB">100</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> while(</span><span style="color: #0000BB">$j</span><span style="color: #007700">--) {<br /> if(</span><span style="color: #0000BB">$j</span><span style="color: #007700">==</span><span style="color: #0000BB">17</span><span style="color: #007700">) goto </span><span style="color: #0000BB">end</span><span style="color: #007700">; <br /> } <br />}<br />echo </span><span style="color: #DD0000">"i = </span><span style="color: #0000BB">$i</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">end</span><span style="color: #007700">:<br />echo </span><span style="color: #DD0000">'j hit 17'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
j hit 17
</pre></div>
</div>
</div><p>
</p>
<p class="para">
</p><div class="example">
<p><b>Example #3 This will not work</b></p>
<div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">goto </span><span style="color: #0000BB">loop</span><span style="color: #007700">;<br />for(</span><span style="color: #0000BB">$i</span><span style="color: #007700">=</span><span style="color: #0000BB">0</span><span style="color: #007700">,</span><span style="color: #0000BB">$j</span><span style="color: #007700">=</span><span style="color: #0000BB">50</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700"><</span><span style="color: #0000BB">100</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> while(</span><span style="color: #0000BB">$j</span><span style="color: #007700">--) {<br /> </span><span style="color: #0000BB">loop</span><span style="color: #007700">:<br /> }<br />}<br />echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$i</span><span style="color: #DD0000"> = </span><span style="color: #0000BB">$i</span><span style="color: #DD0000">"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span>
</span>
</code></div>
</div>
<div class="example-contents para"><p>The above example will output:</p></div>
<div class="example-contents screen">
<div class="cdata"><pre>
Fatal error: 'goto' into loop or switch statement is disallowed in
script on line 2
</pre></div>
</div>
</div><p>
</p>
<blockquote><p><b class="note">Note</b>:
The <i>goto</i> operator is available as of PHP 5.3.
<br />
</p></blockquote>
<p class="para">
<div class="mediaobject">
</p><div class="imageobject">
<img src="images/0baa1b9fae6aec55bbb73037f3016001-xkcd-goto.png" />
</div><p>
</div>
Image courtesy of <a href="http://xkcd.com/292" class="link external">» xkcd</a>
</p>
</div><?php manual_footer(); ?>