0

Probably a stupid question but it just occurred to me. How safe is actually using booleans instead of comparison operators in a condition to check for ANY returned (or not) rows?

People typically do this:

if(mysqli_num_rows($res) > 0) {...} 

...or...

if(mysqli_num_rows($res) < 1) {...}

...but hardly anybody does this:

if(mysqli_num_rows($res)) {...}

...or...

if(!mysqli_num_rows($res)) {...}

I'm not asking if it works or not, I know it does and I know it's legit. But how come this isn't a common practice seeing people tenaciously resort to comparison operators all the way just to see if their query returns more than zero (true) or less than one/zero (false) result? I've tried to envision all possible schemes and failed to detect any pitfalls. All I see is that it's plain convenient, shorter and probably faster, too, although negligibly at that. So, what's the catch? Just a convention?

Puddintane
  • 5
  • 1
  • 3
  • It's just a matter of preference. It's easier to read when you have `>0` but it's not necessary. – Dharman Jun 30 '22 at 07:35
  • That's a PHP question; num_rows() returned a value; at that point the variations you mention are virtually identical in speed. I never use num_rows() since I already have an array of the results; I use `count($rows)` to for testing. – Rick James Jul 01 '22 at 04:15

0 Answers0