View Single Post
  #3 (permalink)  
Old May 24th, 2004, 03:59 PM
richard.york's Avatar
richard.york richard.york is offline
Wrox Author
Points: 5,386, Level: 31
Points: 5,386, Level: 31 Points: 5,386, Level: 31 Points: 5,386, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,697
Thanks: 0
Thanked 1 Time in 1 Post
Default

It's called the ternary operator, and its an expression, if / else if / else are control structures, so technically it isn't just a shortcut for if/else

The manual shows an example like this:

<?php
$first ? $second : $third;
?>

Each of the variables represents a sub expression,
If the value $first subexpression evaluates to TRUE, the code contained in $second subexpression is evaluated if the $first subexpression evaluates as FALSE the $third subexpression is evaluated. Commonly you'll find this in a use that is representable by a simple if/else structure.

if ($foo == TRUE) {

    $bar = 1;
} else {

    $bar = 0;
}

Is the same thing as saying:

$bar = ($foo == TRUE)? 1 : 0;

You can find it in the PHP manual here:
http://www.php.net/manual/en/language.expressions.php

Regards,
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
Reply With Quote