Sophie

Sophie

distrib > Mageia > 4 > i586 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 1229

php-manual-en-5.5.7-1.mga4.noarch.rpm

<!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">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>A&nbsp;is&nbsp;equal&nbsp;to&nbsp;5<br /><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div>
 </p>
 <p class="simpara">
  In the above example, the HTML block &quot;A is equal to 5&quot; 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">&lt;?php<br /></span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;equals&nbsp;5"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"..."</span><span style="color: #007700">;<br />elseif&nbsp;(</span><span style="color: #0000BB">$a&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">6</span><span style="color: #007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;equals&nbsp;6"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"!!!"</span><span style="color: #007700">;<br />else:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;neither&nbsp;5&nbsp;nor&nbsp;6"</span><span style="color: #007700">;<br />endif;<br /></span><span style="color: #0000BB">?&gt;</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>