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.if.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.if.php',
   
1 => 'if',
  ),
 
'up' =>
  array (
   
0 => 'language.control-structures.php',
   
1 => 'Control Structures',
  ),
 
'prev' =>
  array (
   
0 => 'control-structures.intro.php',
   
1 => 'Introduction',
  ),
 
'next' =>
  array (
   
0 => 'control-structures.else.php',
   
1 => 'else',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="control-structures.if" class="sect1">
 <h2 class="title"><i>if</i></h2>
 <p class="para">
  The <i>if</i> construct is one of the most important
  features of many languages, PHP included.  It allows for
  conditional execution of code fragments.  PHP features an
  <i>if</i> structure that is similar to that of C:
  </p><div class="informalexample">
   <div class="example-contents programlisting">
<div class="cdata"><pre>
if (expr)
  statement
</pre></div>
   </div>

  </div><p>
 </p>
 <p class="simpara">
  As described in <a href="language.expressions.php" 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 <b><tt class="constant">TRUE</tt></b>,
  PHP will execute <span class="replaceable">statement</span>, and if it evaluates
  to <b><tt class="constant">FALSE</tt></b> - it&#039;ll ignore it. More information about what values evaluate
  to <b><tt class="constant">FALSE</tt></b> can be found in the <a href="language.types.boolean.php#language.types.boolean.casting" class="link">&#039;Converting to boolean&#039;</a>
  section.
 </p>
 <p class="para">
  The following example would display <span class="computeroutput">a is bigger
  than b</span> if <var class="varname">$a</var> is bigger
  than <var class="varname">$b</var>:
  </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">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;bigger&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <p class="para">
  Often you&#039;d want to have more than one statement to be executed
  conditionally.  Of course, there&#039;s no need to wrap each statement
  with an <i>if</i> 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">$a</var> is bigger than
  <var class="varname">$b</var>, and would then assign the value of
  <var class="varname">$a</var> into <var class="varname">$b</var>:
  </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">&gt;&nbsp;</span><span style="color: #0000BB">$b</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"a&nbsp;is&nbsp;bigger&nbsp;than&nbsp;b"</span><span style="color: #007700">;<br />&nbsp;&nbsp;</span><span style="color: #0000BB">$b&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$a</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

  </div><p>
 </p>
 <p class="simpara">
  <i>If</i> statements can be nested infinitely within other
  <i>if</i> statements, which provides you with complete
  flexibility for conditional execution of the various parts of your
  program.
 </p>
</div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites