<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>goto</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.include-once.html">include_once</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.functions.html">Functions</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.goto" class="sect1"> <h2 class="title"><em>goto</em></h2> <p class="verinfo">(PHP 5 >= 5.3.0)</p> <p class="para"> <div class="mediaobject"> <div class="imageobject"> <img src="images/0baa1b9fae6aec55bbb73037f3016001-xkcd-goto.png" alt="What's the worse thing that could happen if you use goto?" width="740" height="201" /> </div> </div> Image courtesy of <a href="http://xkcd.com/292" class="link external">» xkcd</a> </p> <p class="para"> The <em>goto</em> 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 <em>goto</em> followed by the desired target label. This is not a full unrestricted <em>goto</em>. 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 <em>goto</em> in place of a multi-level <em>break</em>. </p> <p class="para"> <div class="example" id="example-147"> <p><strong>Example #1 <em>goto</em> example</strong></p> <div class="example-contents"> <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"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Bar </pre></div> </div> </div> </p> <p class="para"> <div class="example" id="example-148"> <p><strong>Example #2 <em>goto</em> loop example</strong></p> <div class="example-contents"> <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"><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 class="para"> <div class="example" id="example-149"> <p><strong>Example #3 This will not work</strong></p> <div class="example-contents"> <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"><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> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <em>goto</em> operator is available as of PHP 5.3. </p> </p></blockquote> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.include-once.html">include_once</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.functions.html">Functions</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>