简体   繁体   中英

Comparison operators - are there no greater/less and also same data type operator?

Probably answered 1000 times, but it was very hard to search for. This is a general question, I don't have a specific problem that needs solving. Eg I know there's workarounds I can use.

$a = "true";
if($a > 0) echo "the number is greater than 0";

This evaluates to true, probably because true = 1, and 1 is bigger than 0.

But I don't want the above to evaluate to true, because true is a boolean/string, not a number.

If I was comparing if it was equal to, I could just use === to make sure it was the same data type also, but there doesn't seem to be a similar way to compare using the greater than/less than operators? (eg >> ?)

You probably have noticed that you must write $a = TRUE and not $a ="true" because this whould be a string.

you can check $a with is_integer function

$a = TRUE;

if($a > 0 && is_integer($a) ) {
  echo "the number is greater than 0";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM