Is conversion necessary for integer comparisons in PHP?
Throughout much of the code I review, I often see things such as:
// $myvar could be anything
if (intval($myvar) > 0) {
// do stuff
}
Or similarly:
if ($myvar != null && intval($myvar) > 0) {
// do stuff
}
In an ongoing quest to produce more elegant and less-wasteful code, I am wondering if it is ne开发者_如何学Gocessary to call intval(), for example, when performing a numeric comparison. My understanding from reading the documentation and from various responses here is that when a numeric comparison is asked for, it checks the type of the var and then performs either an integer cast or a string conversion depending on what it finds -- or are these the same as far as PHP is concerned, operations-wise?
Based on that, it seems the least expensive way is to tell PHP to cast it directly as an integer, as such:
if ((int)$myvar > 0) {
// do stuff
}
However, I almost never see it written that way. Am I missing anything? Does PHP automatically do this any time it seems a comparison operator?
I realize in 99.9% of situations it is sufficient to just perform the comparison and let PHP type juggle on its own, but I see this so often that either I am missing something or many others are!
You haven't given a reason to cast at all:
$myvar > 0
will have the same effect. Either way, non-numeric strings can be coerced to 0 (this is one of PHP's many strange design choices), and numeric strings are coerced to the number they represent. Thus after:
$myvar = "foo";
the following are identical (and false):
(int) $myvar > 0
$myvar > 0
Of course, casting to int
may be useful in other situations. Another useful function is is_numeric
, which checks for a number or numeric string.
If in doubt, do the most reliable and predictable thing. In this case, that means doing the casting yourself.
if ((int) $myvar > 0) {
}
will give the most reliable results. Furthermore it is very legible and obvious what you're doing. I would definitely prefer to read explicit code than code that relied upon magic PHP behaviour.
As an example, take the instance where you are comparing two variables, rather than an integer and a variable.
if ($a > $b) {
This will do an integer comparison if either $a
or $b
is an integer. If they are both strings, the comparison will be a string-based one. If you do the casting yourself, you know what comparison is being done every time.
I prefer to always know what types of data I'm dealing with, and only convert when necessary.
If you're writing a function that should take ints as arguments, you don't need to convert them. Just check the arguments' types with is_int
, and use assert
or trigger_error
to show a runtime error if they aren't integers. That way you'll know right away if someone is misusing your functions.
On the other hand, if you're getting input from the user (like $_GET or $_POST), it will probably be a string. So before you use it for any calculations, you should convert it to an integer.
Use (int) as a cheap and effective filtering mechanism. If it comes across the value:
$var = "mm";
echo (int)$var ;
will cause it to become 0 which will fail a test such as:
if( (int)$var === 0){
// it must be an integer greater than zero
}
If $var is not set, then yes you will throw a warning, hence you use is with isset()
精彩评论