问题
In one bit of code I'm working on, I need to pull a value from the database and check if it is 0
. Originally, I had written this as:
if ($myVal == 0) { ...
But when I looked at it again today, I realised a bug there:
var_dump("foo" == 0); // bool(true)
// and while we're here...
var_dump(intval("foo")); // int(0)
Since this value is coming from the database, that usually means it will be a string, so I suppose I could do this:
if ($myVal === "0")
but it seems counter-intuitive since I actually want to be dealing with an integer, and this would appear to show that we're working with strings. (I hope that makes sense to you.) Also, it shouldn't be unexpected that any given DB accessor would cast values to their appropriate type, given the field type.
What method do you use to check for a value of 0 when it could be a string or an int?
回答1:
Short version:
if ("$myVal" === '0')
or
if (strval($myVal) === '0')
Long version:
if ($myVal === 0 || $myVal === '0')
回答2:
The best I've got currently is this:
is_numeric($myVal) && $myVal == 0
回答3:
If the values you get back from the database are strings, but the original datatype is integer, then you probably want your DAO (Data Access Object, or whatever it is that's pulling the data) to cast things to data types you expect. That way, the PHP code that's looking at the values is looking at the data type it expects.
The trouble is, of course, that there isn't a 1-1 correspondence between the data types the DB defines, and the data types PHP defines. However, for the most part this isn't a problem, since there's equivalent types for most of the types we use normally: int, float, bool, string.
The upshot is, that between the place where you query the database, and the place where you check those values, those values should be cast to the data types you expect. I just tested with mysql_fetch_object() on a MySQL table containing an int(10) and a varchar(50); both fields always were pulled in as type "string" by PHP. So if you want one of the fields treated as an int, cast it to an int before you do anything with it. For example:
$rs = mysql_query("SELECT * FROM table");
while ($row = mysql_fetch_object($rs)) {
$RealRow->id = (int)$row->id;
$RealRow->name = $row->name;
// etc.
}
// ... later ...
if ($RealRow->status == 0) {
// do something
}
The query and RealRow stuff should be inside a class/method, so that it's encapsulated; that way, ANY part of your code that gets data from table
will always get the $RealRow object instead, which will have everything set to the proper data types. Of course, if you change your data types, this requires manually editing the DAO to deal with the casting; it would also be possible to query the DB to get the type of each field (with a "SHOW COLUMNS FROM table"), and automatically cast the fields to the proper data type.
回答4:
I think you should just use
if ($myVal == 0)
I don't think it's necessary to worry about whether $myVal is "foo". You've said yourself that this value is only ever going to be a number.
In other words, if you ever experience $myVal being "foo", the bug is not that "foo" == 0, the bug is that $myVal is "foo", when it's supposed to be a number.
回答5:
Try using intval() to cast the string to an int
if( intval($myVal) == 0 )
回答6:
if( ('integer' === gettype($is_anonymous) && $is_anonymous === 0 )
||
('string' === gettype($is_anonymous) && $is_anonymous === '0')
) {
echo 'matches';
}
来源:https://stackoverflow.com/questions/875854/how-best-to-compare-to-0-in-php