<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = 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.alternative-syntax.php',
    1 => 'Alternative syntax for control structures',
    2 => 'Alternative syntax for control structures',
  ),
  'up' => 
  array (
    0 => 'language.control-structures.php',
    1 => 'Control Structures',
  ),
  'prev' => 
  array (
    0 => 'control-structures.elseif.php',
    1 => 'elseif/else if',
  ),
  'next' => 
  array (
    0 => 'control-structures.while.php',
    1 => 'while',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'language/control-structures/alternative-syntax.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="control-structures.alternative-syntax" class="sect1">
 <h2 class="title">Alternative syntax for control structures</h2>
 <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p>
 <p class="para">
  PHP offers an alternative syntax for some of its control
  structures; namely, <code class="literal">if</code>,
  <code class="literal">while</code>, <code class="literal">for</code>,
  <code class="literal">foreach</code>, and <code class="literal">switch</code>.
  In each case, the basic form of the alternate syntax is to change
  the opening brace to a colon (:) and the closing brace to
  <code class="literal">endif;</code>, <code class="literal">endwhile;</code>,
  <code class="literal">endfor;</code>, <code class="literal">endforeach;</code>, or
  <code class="literal">endswitch;</code>, respectively.
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?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">?&gt;<br /></span>A is equal to 5<br /><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endif; </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
  <code class="literal">if</code> statement written in the alternative syntax.  The
  HTML block would be displayed only if <var class="varname">$a</var> is equal to 5.
 </p>
 <p class="para">
  The alternative syntax applies to <code class="literal">else</code> and
  <code class="literal">elseif</code> as well.  The following is an
  <code class="literal">if</code> structure with <code class="literal">elseif</code> and
  <code class="literal">else</code> 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 (</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">?&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>
 <div class="warning"><strong class="warning">Warning</strong>
  <p class="para">
   Any output (including whitespace) between a <code class="literal">switch</code>
   statement and the first <code class="literal">case</code> will result in a syntax
   error. For example, this is invalid:
  </p>
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">switch (</span><span style="color: #0000BB">$foo</span><span style="color: #007700">): </span><span style="color: #0000BB">?&gt;<br /></span>    <span style="color: #0000BB">&lt;?php </span><span style="color: #007700">case </span><span style="color: #0000BB">1</span><span style="color: #007700">: </span><span style="color: #0000BB">?&gt;<br /></span>    // ...<br /><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endswitch; </span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
  <p class="para">
   Whereas this is valid, as the trailing newline after the
   <code class="literal">switch</code> statement is considered part of the closing
   <code class="literal">?&gt;</code> and hence nothing is output between the
   <code class="literal">switch</code> and <code class="literal">case</code>:
  </p>
  <div class="informalexample">
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">switch (</span><span style="color: #0000BB">$foo</span><span style="color: #007700">): </span><span style="color: #0000BB">?&gt;<br />&lt;?php </span><span style="color: #007700">case </span><span style="color: #0000BB">1</span><span style="color: #007700">: </span><span style="color: #0000BB">?&gt;<br /></span>    ...<br /><span style="color: #0000BB">&lt;?php </span><span style="color: #007700">endswitch; </span><span style="color: #0000BB">?&gt;</span></span></code></div>
   </div>

  </div>
 </div>
 <p class="para">
  See also <a href="control-structures.while.php" class="link">while</a>,
  <a href="control-structures.for.php" class="link">for</a>, and <a href="control-structures.if.php" class="link">if</a> for further examples.
 </p>
</div><?php manual_footer($setup); ?>