<!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>break</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.foreach.html">foreach</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.continue.html">continue</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.break" class="sect1"> <h2 class="title"><em>break</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="simpara"> <em>break</em> ends execution of the current <em>for</em>, <em>foreach</em>, <em>while</em>, <em>do-while</em> or <em>switch</em> structure. </p> <p class="simpara"> <em>break</em> accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of. </p> <p class="para"> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$arr </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'one'</span><span style="color: #007700">, </span><span style="color: #DD0000">'two'</span><span style="color: #007700">, </span><span style="color: #DD0000">'three'</span><span style="color: #007700">, </span><span style="color: #DD0000">'four'</span><span style="color: #007700">, </span><span style="color: #DD0000">'stop'</span><span style="color: #007700">, </span><span style="color: #DD0000">'five'</span><span style="color: #007700">);<br />while (list(, </span><span style="color: #0000BB">$val</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$arr</span><span style="color: #007700">)) {<br /> if (</span><span style="color: #0000BB">$val </span><span style="color: #007700">== </span><span style="color: #DD0000">'stop'</span><span style="color: #007700">) {<br /> break; </span><span style="color: #FF8000">/* You could also write 'break 1;' here. */<br /> </span><span style="color: #007700">}<br /> echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$val</span><span style="color: #DD0000"><br />\n"</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #FF8000">/* Using the optional argument. */<br /><br /></span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">;<br />while (++</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br /> switch (</span><span style="color: #0000BB">$i</span><span style="color: #007700">) {<br /> case </span><span style="color: #0000BB">5</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"At 5<br />\n"</span><span style="color: #007700">;<br /> break </span><span style="color: #0000BB">1</span><span style="color: #007700">; </span><span style="color: #FF8000">/* Exit only the switch. */<br /> </span><span style="color: #007700">case </span><span style="color: #0000BB">10</span><span style="color: #007700">:<br /> echo </span><span style="color: #DD0000">"At 10; quitting<br />\n"</span><span style="color: #007700">;<br /> break </span><span style="color: #0000BB">2</span><span style="color: #007700">; </span><span style="color: #FF8000">/* Exit the switch and the while. */<br /> </span><span style="color: #007700">default:<br /> break;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <table class="doctable table"> <caption><strong>Changelog for <em>break</em></strong></caption> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.4.0</td> <td> <em>break 0;</em> is no longer valid. In previous versions it was interpreted the same as <em>break 1;</em>. </td> </tr> <tr> <td>5.4.0</td> <td> Removed the ability to pass in variables (e.g., <em>$num = 2; break $num;</em>) as the numerical argument. </td> </tr> </tbody> </table> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.foreach.html">foreach</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.continue.html">continue</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>