PHP if statement syntax

Examples of using the “if” command in PHP.

The simplest if statement executes command(s) if a condition evaluates as true:

if ($var > 0 ) {
// code here run if $var is positive
echo "Square root is: ".sqrt($var);
}

An optional else block can be added to execute code if the test condition evaluates as false. Multiple lines of code must be enclosed within { braces } while a single statement can be used in an if statement without braces.

if ($var >= 0 )
echo "Square root is: ".sqrt($var);
else {
$negvar = -1*$var;
echo "Square root is: ".sqrt($var)."i";
}

If statements can be nested:

if ($var < 10)
echo "var is less than 10";
elseif ($var < 20)
echo "var is between 10 and 20";
elseif ($var < 30)
echo "var is between 20 and 30";
else
echo "var is greater than 30";

A compact (if cryptic) shorthand is useful:

echo "var is ".($var < 0 ? "negative" : "positive");

is equivalent to:

echo "var is ";
if ($var < 0)
echo "negative";
else
echo "positive";

The shorthand can be considered (expression ? true_value : false_value) where 'expression' is evaluated and, if true, the 'true_value' is returned, otherwise the 'false_value' is returned.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

8 Responses to “PHP if statement syntax”

  1. January 09, 2009 at 1:16 pm, Tom Elders said:

    I think describing the ternary operator “?” as an if statement is a bit misleading. For example, inside a while loop:

    while($row = mysql_fetch_row):
    if($condition = true) { $something = $value }
    endwhile;

    that works. However, consider the following

    while($row = mysql_fetch_row):
    $something = ($condition = $true) ? $value : null;
    endwhile;

    If the condition is met, everything works fine, but on the next iteration of the loop, if the condition is not met, $something is set to null, which is probably not the desired effect 9 times out of 10.

    Reply

  2. January 16, 2009 at 8:53 pm, peace love said:

    Thats a great shorthand, and gives good readability!

    Reply

    • May 20, 2009 at 11:35 pm, Sagar said:

      yes, ternary condition does give a problem

      I would advise the following as well

      while($row = mysql_fetch_row):
      if($condition = true) { $something = $value }
      endwhile;

      Reply

      • May 20, 2009 at 11:44 pm, Sagar said:

        My above suggestion is the same as what Tom Elders suggested. I support it

        Reply

  3. August 19, 2009 at 12:59 pm, Name said:

    I am a novie in php programming.I was looking for “elseif” in php and this writ up heloped me to do it..

    Thanking you
    Anand V Mohan

    Reply

    • August 22, 2009 at 4:50 pm, davak said:

      Glad we could help!

      Reply

  4. September 18, 2010 at 10:34 am, php programming said:

    Done a excellent job in this blog, Alternatively create a great blog for the readers specially for PHP programming. Thanks.

    Reply

  5. September 25, 2010 at 3:26 am, bajick said:

    This is great!!! Nway, is there any way to shorthand the nested if statement?

    Reply

Leave a Reply