Source of: /manual/en/control-structures.alternative-syntax.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = 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',
),
'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',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<div id="control-structures.alternative-syntax" class="sect1">
<h2 class="title">Alternative syntax for control structures</h2>
<p class="para">
PHP offers an alternative syntax for some of its control
structures; namely, <i>if</i>,
<i>while</i>, <i>for</i>,
<i>foreach</i>, and <i>switch</i>.
In each case, the basic form of the alternate syntax is to change
the opening brace to a colon (:) and the closing brace to
<i>endif;</i>, <i>endwhile;</i>,
<i>endfor;</i>, <i>endforeach;</i>, or
<i>endswitch;</i>, respectively.
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
<p class="simpara">
In the above example, the HTML block "A is equal to 5" is nested within an
<i>if</i> 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 <i>else</i> and
<i>elseif</i> as well. The following is an
<i>if</i> structure with <i>elseif</i> and
<i>else</i> in the alternative format:
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
</p>
<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(); ?>