<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>if</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.intro.html">Introduction</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.else.html">else</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="control-structures.if" class="sect1"> <h2 class="title"><em>if</em></h2> <p class="verinfo">(PHP 4, PHP 5)</p> <p class="para"> The <em>if</em> construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an <em>if</em> structure that is similar to that of C: <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> if (expr) statement </pre></div> </div> </div> </p> <p class="simpara"> As described in <a href="language.expressions.html" class="link">the section about expressions</a>, <span class="replaceable">expression</span> is evaluated to its Boolean value. If <span class="replaceable">expression</span> evaluates to <strong><code>TRUE</code></strong>, PHP will execute <span class="replaceable">statement</span>, and if it evaluates to <strong><code>FALSE</code></strong> - it'll ignore it. More information about what values evaluate to <strong><code>FALSE</code></strong> can be found in the <a href="language.types.boolean.html#language.types.boolean.casting" class="link">'Converting to boolean'</a> section. </p> <p class="para"> The following example would display <span class="computeroutput">a is bigger than b</span> if <var class="varname"><var class="varname">$a</var></var> is bigger than <var class="varname"><var class="varname">$b</var></var>: <div class="informalexample"> <div class="example-contents"> <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">$b</span><span style="color: #007700">)<br /> echo </span><span style="color: #DD0000">"a is bigger than b"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> 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 <em>if</em> clause. Instead, you can group several statements into a statement group. For example, this code would display <span class="computeroutput">a is bigger than b</span> if <var class="varname"><var class="varname">$a</var></var> is bigger than <var class="varname"><var class="varname">$b</var></var>, and would then assign the value of <var class="varname"><var class="varname">$a</var></var> into <var class="varname"><var class="varname">$b</var></var>: <div class="informalexample"> <div class="example-contents"> <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">$b</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"a is bigger than b"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="simpara"> <em>If</em> statements can be nested infinitely within other <em>if</em> statements, which provides you with complete flexibility for conditional execution of the various parts of your program. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="control-structures.intro.html">Introduction</a></div> <div class="next" style="text-align: right; float: right;"><a href="control-structures.else.html">else</a></div> <div class="up"><a href="language.control-structures.html">Control Structures</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>