PHP Ternary Operator

PHP ternary operator isn’t beautiful for reading reason

It’s also make confuse, but for the sake of simple it’s OK 🙂

Syntax :

( expr1 ) ? ( expr2 ) : (expr3)

If expr1 return true, then it evaluates to ( expr2 )
If expr1 return false, then it evaluates to ( expr3 )

it’s similar to if..else

if(true) {

code 1

}

else {

code2

}

example :

// ternary operator

$year=2006;
$james_bond_title = ($year==2006) ? “Casino Royale” : ” I forgot “;

echo “James Bond Movie Title in 2006 is : $james_bond_title”;

//if … else

if($year==2006){

$james_bond_title=”Casino Royale”;

}

else {

$james_bond_title=”I forgot “;

}

echo $james_bond_title;

Which one is your favorite ?