alecwhardy
alecwhardy

Reputation: 2718

Return TRUE if counter is greater than 0?

In PHP I am trying to return TRUE if $counter is greater than 0. Would using a ternary operator work in this case. Here is the original code:

if($counter>0){return TRUE;}else{return FALSE;}

could I condense that down to

return $counter>0?TRUE:FALSE

Thanks

Upvotes: 5

Views: 11405

Answers (3)

simhumileco
simhumileco

Reputation: 34603

Yes you could condense that, but you can sometime consider also that:

return is_int($counter) && $counter > 0;

This expression checks if it is greater than zero and in addition if $counter is integer.

Upvotes: 1

Andreas
Andreas

Reputation: 2678

return ($counter > 0) ? TRUE : FALSE;

If you like, yes you can!

Upvotes: 3

Joe
Joe

Reputation: 82624

You could condense it to return $counter>0

Because that is a Boolean expression itself.

Upvotes: 19

Related Questions