search for in the  
<Type Operatorselse>
Last updated: Thu, 19 May 2005

Chapter 16. Control Structures

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.

if

The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:

<?php
if (expression)
    statement
?>

As described in the section about expressions, expr is evaluated to its Boolean value. If expr evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.

The following example would display a is bigger than b if $a is bigger than $b:

<?php
if ($a > $b)
   echo
"a is bigger than b";
?>

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 if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:

<?php
if ($a > $b) {
   echo
"a is bigger than b";
  
$b = $a;
}
?>

If statements can be nested indefinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.



User Contributed Notes
Control Structures
macristo[AT]gmx[DOT]net
06-May-2005 02:01
It is not mandatory to use the parentheses in an if-statement.
This is an if-statement on one line and it's perfectly valid code:

<?php
$a
= "foo";
if (
"foo" == $a) echo "bar";
?>

will output:
"bar"
dotwho at nospam dot mac dot com
17-Feb-2005 07:12
As of php 5.01:

if using the short cut if - else syntax:

condition ? true : false;

be careful. inside of condition the and / or operators do not work as expected. use instead && / ||.

Example:
<?php
$sText
= (!isset($searchText) or $sText === "" "NULL" : $sText);
?>
Always sets $sText to "1".

<?php
$sText
= (!isset($searchText) || $sText === "" "NULL" : $sText);
?>
Sets $sText appropriately.
niels dot laukens at tijd dot com
26-Dec-2004 09:49
For the people that know C: php is lazy when evaluating expressions. That is, as soon as it knows the outcome, it'll stop processing.

<?php
if ( FALSE && some_function() )
   echo
"something";
// some_function() will not be called, since php knows that it will never have to execute the if-block
?>

This comes in nice in situations like this:
<?php
if ( file_exists($filename) && filemtime($filename) > time() )
  
do_something();
// filemtime will never give an file-not-found-error, since php will stop parsing as soon as file_exists returns FALSE
?>
Job Oberio
21-Oct-2004 03:19
if you want to have two expressions you can
use any logical operators  'and', 'or'

<?php
if ($num > 10 and $num < 15)
       {
           echo
"number is greater than 10 but less than 15";       
       }
?>

another way, you can enclose each expression into
open/close parentheses - ( expression1 ) and (expression2)
so that its easy to analyze your expressions

<?php
if ( ($num > 10) and ($num < 15) )
       {
           echo
"number is greater than 10 but less than 15";       
       }
?>

i prefer using the wordings 'and' instead of '&&' operator
because wordings operators are easy to analyze and easy
to your eye to read especially for long expressions.
the '&&' tends to be confused to $ sign

<Type Operatorselse>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT