BottledTurtles BottledTurtles - 5 months ago 11
PHP Question

PHP Different shorthand if

After checking some shorthand if's i got confused.

$num = 10;
return ($num>0)? 'banned' : 'free';


$num = 10;
return ($num>0 ? 'banned' : 'free');

are both equal or the first one is wrong?


Both of the case are true is their own state.

The first one

return ($num>0)? 'banned' : 'free';

is used when you need to compare two or more conditions as in

return ($num > 0 && $num <= 10) ? 'banned' : 'free'. ' model' ;

Here, ' model' is applied to only false condition.

This can be applied into the second expression as well. The brackets in second expression defined as one expression. as in

return ($num > 0 ? 'banned' : 'free' ) . ' model' ;

Here, ' model' is concatenated to any of the result.

Its totally based on your priority of selecting the bracketss.