Saturday, August 30, 2008

ternary operator ?:

ternary operation is not hard but not easy to use.
it can be short form of if...else and it is used in an expression as follows :
condition ? valueIfTrue : valueIfFalse
therefore we wrote

 echo 'more than 5';
 echo 'not more than 5';
by ternary operator we can use just only
echo ($i>5)?'more than 5':'not more than 5';

or something like
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];
instead of
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];

you can also use ?: instead of switch case too.