<HTML ><HEAD ><TITLE >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="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Array Operators" HREF="language.operators.array.html"><LINK REL="NEXT" TITLE="else" HREF="control-structures.else.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" 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="language.operators.array.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.else.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="control-structures" >Chapter 12. Control Structures</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="control-structures.html#control-structures.if" ><TT CLASS="literal" >if</TT ></A ></DT ><DT ><A HREF="control-structures.else.html" ><TT CLASS="literal" >else</TT ></A ></DT ><DT ><A HREF="control-structures.elseif.html" ><TT CLASS="literal" >elseif</TT ></A ></DT ><DT ><A HREF="control-structures.alternative-syntax.html" >Alternative syntax for control structures</A ></DT ><DT ><A HREF="control-structures.while.html" ><TT CLASS="literal" >while</TT ></A ></DT ><DT ><A HREF="control-structures.do.while.html" ><TT CLASS="literal" >do..while</TT ></A ></DT ><DT ><A HREF="control-structures.for.html" ><TT CLASS="literal" >for</TT ></A ></DT ><DT ><A HREF="control-structures.foreach.html" ><TT CLASS="literal" >foreach</TT ></A ></DT ><DT ><A HREF="control-structures.break.html" ><TT CLASS="literal" >break</TT ></A ></DT ><DT ><A HREF="control-structures.continue.html" ><TT CLASS="literal" >continue</TT ></A ></DT ><DT ><A HREF="control-structures.switch.html" ><TT CLASS="literal" >switch</TT ></A ></DT ><DT ><A HREF="control-structures.declare.html" ><TT CLASS="literal" >declare</TT ></A ></DT ><DT ><A HREF="function.return.html" >return</A ></DT ><DT ><A HREF="function.require.html" ><A HREF="function.require.html" ><B CLASS="function" >require()</B ></A ></A ></DT ><DT ><A HREF="function.include.html" ><A HREF="function.include.html" ><B CLASS="function" >include()</B ></A ></A ></DT ><DT ><A HREF="function.require-once.html" ><A HREF="function.require-once.html" ><B CLASS="function" >require_once()</B ></A ></A ></DT ><DT ><A HREF="function.include-once.html" ><A HREF="function.include-once.html" ><B CLASS="function" >include_once()</B ></A ></A ></DT ></DL ></DIV ><P > Any PHP script is built out of a series of statements. A statement can be an assignment, a function call, a loop, a conditional statement of even a statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, statements can be grouped into a statement-group by encapsulating a group of statements with curly braces. A statement-group is a statement by itself as well. The various statement types are described in this chapter. </P ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.if" ></A ><TT CLASS="literal" >if</TT ></H1 ><P > The <TT CLASS="literal" >if</TT > construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an <TT CLASS="literal" >if</TT > structure that is similar to that of C: <DIV CLASS="informalexample" ><A NAME="AEN4762" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="programlisting" >if (expr) statement</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > As described in <A HREF="language.expressions.html" >the section about expressions</A >, <TT CLASS="replaceable" ><I >expr</I ></TT > is evaluated to its Boolean value. If <TT CLASS="replaceable" ><I >expr</I ></TT > evaluates to <TT CLASS="constant" ><B >TRUE</B ></TT >, PHP will execute <TT CLASS="replaceable" ><I >statement</I ></TT >, and if it evaluates to <TT CLASS="constant" ><B >FALSE</B ></TT > - it'll ignore it. More information about what values evaluate to <TT CLASS="constant" ><B >FALSE</B ></TT > can be found in the <A HREF="language.types.boolean.html#language.types.boolean.casting" >'Converting to boolean'</A > section. </P ><P > The following example would display <TT CLASS="computeroutput" >a is bigger than b</TT > if <TT CLASS="replaceable" ><I >$a</I ></TT > is bigger than <TT CLASS="replaceable" ><I >$b</I ></TT >: <DIV CLASS="informalexample" ><A NAME="AEN4777" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >if ($a > $b) print "a is bigger than b";</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an <TT CLASS="literal" >if</TT > clause. Instead, you can group several statements into a statement group. For example, this code would display <TT CLASS="computeroutput" >a is bigger than b</TT > if <TT CLASS="replaceable" ><I >$a</I ></TT > is bigger than <TT CLASS="replaceable" ><I >$b</I ></TT >, and would then assign the value of <TT CLASS="replaceable" ><I >$a</I ></TT > into <TT CLASS="replaceable" ><I >$b</I ></TT >: <DIV CLASS="informalexample" ><A NAME="AEN4786" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >if ($a > $b) { print "a is bigger than b"; $b = $a; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > <TT CLASS="literal" >If</TT > statements can be nested indefinitely within other <TT CLASS="literal" >if</TT > statements, which provides you with complete flexibility for conditional execution of the various parts of your program. </P ></DIV ></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="language.operators.array.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.else.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Array Operators</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >else</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >