<HTML ><HEAD ><TITLE >Alternative syntax for control structures</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="elseif" HREF="control-structures.elseif.html"><LINK REL="NEXT" TITLE="while" HREF="control-structures.while.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="control-structures.elseif.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.while.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.alternative-syntax" ></A >Alternative syntax for control structures</H1 ><P > PHP offers an alternative syntax for some of its control structures; namely, <TT CLASS="literal" >if</TT >, <TT CLASS="literal" >while</TT >, <TT CLASS="literal" >for</TT >, <TT CLASS="literal" >foreach</TT >, and <TT CLASS="literal" >switch</TT >. In each case, the basic form of the alternate syntax is to change the opening brace to a colon (:) and the closing brace to <TT CLASS="literal" >endif;</TT >, <TT CLASS="literal" >endwhile;</TT >, <TT CLASS="literal" >endfor;</TT >, <TT CLASS="literal" >endforeach;</TT >, or <TT CLASS="literal" >endswitch;</TT >, respectively. <DIV CLASS="informalexample" ><A NAME="AEN4856" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($a == 5): ?> A is equal to 5 <?php endif; ?></PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > In the above example, the HTML block "A is equal to 5" is nested within an <TT CLASS="literal" >if</TT > statement written in the alternative syntax. The HTML block would be displayed only if $a is equal to 5. </P ><P > The alternative syntax applies to <TT CLASS="literal" >else</TT > and <TT CLASS="literal" >elseif</TT > as well. The following is an <TT CLASS="literal" >if</TT > structure with <TT CLASS="literal" >elseif</TT > and <TT CLASS="literal" >else</TT > in the alternative format: <DIV CLASS="informalexample" ><A NAME="AEN4866" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >if ($a == 5): print "a equals 5"; print "..."; elseif ($a == 6): print "a equals 6"; print "!!!"; else: print "a is neither 5 nor 6"; endif;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > See also <A HREF="control-structures.while.html" >while</A >, <A HREF="control-structures.for.html" >for</A >, and <A HREF="control-structures.html#control-structures.if" >if</A > for further examples. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="control-structures.elseif.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="control-structures.while.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >elseif</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >while</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >