<!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>Alternative syntax for control structures</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</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.alternative-syntax" class="sect1"> <h2 class="title">Alternative syntax for control structures</h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> PHP offers an alternative syntax for some of its control structures; namely, <em>if</em>, <em>while</em>, <em>for</em>, <em>foreach</em>, and <em>switch</em>. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to <em>endif;</em>, <em>endwhile;</em>, <em>endfor;</em>, <em>endforeach;</em>, or <em>endswitch;</em>, respectively. <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php </span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">5</span><span style="color: #007700">): </span><span style="color: #0000BB">?><br /></span>A is equal to 5<br /><span style="color: #0000BB"><?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="simpara"> In the above example, the HTML block "A is equal to 5" is nested within an <em>if</em> statement written in the alternative syntax. The HTML block would be displayed only if <var class="varname"><var class="varname">$a</var></var> is equal to 5. </p> <p class="para"> The alternative syntax applies to <em>else</em> and <em>elseif</em> as well. The following is an <em>if</em> structure with <em>elseif</em> and <em>else</em> in the alternative format: <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">5</span><span style="color: #007700">):<br /> echo </span><span style="color: #DD0000">"a equals 5"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"..."</span><span style="color: #007700">;<br />elseif (</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">6</span><span style="color: #007700">):<br /> echo </span><span style="color: #DD0000">"a equals 6"</span><span style="color: #007700">;<br /> echo </span><span style="color: #DD0000">"!!!"</span><span style="color: #007700">;<br />else:<br /> echo </span><span style="color: #DD0000">"a is neither 5 nor 6"</span><span style="color: #007700">;<br />endif;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Mixing syntaxes in the same control block is not supported. </p> </p></blockquote> <p class="para"> See also <a href="control-structures.while.html" class="link">while</a>, <a href="control-structures.for.html" class="link">for</a>, and <a href="control-structures.if.html" class="link">if</a> for further examples. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.elseif.html">elseif/else if</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.while.html">while</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>