laserlight wrote:The casting to bool version is less clear than comparison with 0. Instead of thinking "check if the number of rows returned is greater than zero (or non-zero)", you think "what happens when the return value is converted to a boolean value?"
I doubt I'd actually use the idiom I suggested (for fun) in my previous reply, but if you think about it, it's every bit as "clear" as the very typical idiom you see people use all the time:
if(count($some_array))
{
// do something
}
It's exactly the same thing, really: you have to understand that count() will return an integer from 0 - n, and that number will be automatically cast by the IF's condition expression to a boolean false if the result is zero, else it will be true. What you are really doing with the above statement is...
if( (bool) count($some_array) ) {
...but PHP does the type conversion automatically in such a situation, so the explicit casting via "(bool)" would be redundant.
Now for the novice programmer who doesn't even know what "type casting" is, that may be a different story as to what is clear and what is not. But if we've caused one or two of those novices to dig into the manual a bit to figure out wtf I'm talking about, then "mission accomplished!" 🙂
PS: Guess it would be polite of me to include this link for any who want to do some digging: http://www.php.net/manual/en/language.types.type-juggling.php