downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

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">&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>
 <p class="simpara">
  In the above example, the HTML block &quot;A is equal to 5&quot; 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">&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>
 </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(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites