views:

32

answers:

2
if( $a == $b) { return true;} 
else { return false;}

how to write a ternary operater for the following ?

is this the way

if($a == $b)? return true; : return false;
+6  A: 

You don't need the ternary operator at all. Just return this and you'll get the correct true or false value.

return $a == $b;
BoltClock
+3  A: 

You could just

return ($a == $b)

But if you really want to use the operator

return ( ($a == $b) ? true : false)
djna
incase instead of return true i wanna return a variable... can i do thisreturn ( ($a == $b) ? $variable : false)
Harsha M V
yes, in C ? A : B both A and B can be arbitrary expressions, you could have a single variable as you suggest, or even complex sums. Your idea of using a varibale makes for readable code.
djna
i am using php.
Harsha M V