PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<Array Operatorselse>
view the version of this page
Last updated: Thu, 21 Aug 2003

Chapter 11. 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:

if (expr)
    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)
    print "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) {
    print "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.



add a note add a note User Contributed Notes
Control Structures
molotov at molotov dot eu dot org
30-Sep-2003 02:25
You definitely can write a simple control structure like this:
<?php
if ($var 5)
{
   
$answ $var1;
}
else
{
   
$answ $var2;
}
?>

But there is a much shorter way:

<?php
$answ 
= ( $var $var1 $var2 );
/* $result = ( $expression ? $return_if_true : $return_if_false ) */
?>
Athon Solo
05-Sep-2003 05:41
If you want to check for multiple conditions you can do it in atleast 2 ways:

1. Nested if statements:
<?php

 
if ($var1 == $var2) {
    if (
$var1 != $var3) {
      echo 
$var1 ." = "$var2 ." != "$var3;
    }
  }
?>

2. All in one line:
<?php

 
if (($var1 == $var2) AND ($var1 != $var3)) {
    echo 
$var1 ." = "$var2 ." != "$var3;
  }
?>

Note: you can also use the 'OR' keyword between conditions.
You can have as many conditions as you want.
For full details on 'operators' like AND and OR, please see http://www.php.net/language.operators
maurizio at zilli dot com
12-Apr-2002 04:14
You can also use this if/else condition structure
to control an index navigation page statement
and extract partial rows from an array.

// Init to control pointer into the array

$setlimit = 10; // rows limit
$pointer = 0; // this is the start pointer value
$next = $pointer + $setlimit; // increment the pointer
$prev= $next - $setlimit; // decrement the pointer
$total_rows = mysql_num_rows($myrows); // total rows

// You're at the beginning
if (($pointer == 0) && ($total_rows > $setlimit)):

echo"$next >>";

// You're in the middle
elseif ($next < $total_rows):

echo"<< $prev";
echo"$pointer";
echo"$next >>";

// You're in a selection with only one row
elseif (($next == $total_rows) && ($next > $total_rows)):

// You're at the end
else:

echo"<< $prev";

endif;

<Array Operatorselse>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: http://php.mirrors.ilisys.com.au/
Last updated: Sat 01 Nov 2003 04:13:36 EST EST