问题
is_numeric, intval, ctype__digit.. can you rely on them?
or do i have to use regex?
function isNum($str) {
return (preg_match("/^[0-9]+$/", $str));
}
what do you guys think? am i stupid?
回答1:
One important difference between ctype_digit
and is_numerict
is negative values and float number.
is_numeric(-10)
will return true
whereas
'ctype_digit(-10)' will be false
also ctype_digit(12.50)
will return false
whereas
is_numeric(12.50)
will be true
So both of them are handy depends on the context of your domain logic.
回答2:
is_numeric, intval, and ctype_digit all do very different things.
is_numeric will tell you if the contents of the variable are numeric (i.e. true if it is a floating point or integer value).
intval attempts to convert a string of numbers to an integer value
ctype_digit will tell you if a string contains nothing but numeric characters (will perform the same check as you isNum function).
best approach is probably to check if is_numeric is true and then use something along the lines of settype($myvalue, 'integer') or intval($myvalue);
回答3:
You named two kinds of functions:
A Validator checks if the given value has the given characteristics and returns either true or false.
is_numeric, the ctype_* functions and your isNum
function are validating functions as they just tell you if a value is valid or not.
A Filter changes the given value in such way that the new value has the given characteristics and thus will be valid.
intval and the filter_* functions are filtering functions as they always will return valid values that would pass a validator.
回答4:
You can also use the new filter functions.
if (!$var = filter_var($var, FILTER_VALIDATE_INT)) {
die('Not an int!');
}
echo "Var has the value $var.\n";
Best used when filtering input from cli, web client, etc. List of filters here.
回答5:
Follow the docs. is_numeric
will always be available and merely validates that you have a string that could be considered a number by PHP. The ctype_*
functions are a little bit narrower in scope but should also always be available.
A RegEx is, IMO, overkill for such checks.
回答6:
I am not an expert on PHP, but have you considered writing a data driven unit test to gain a concrete understanding of those functions. If you are uncertain about their reliability and the documentation is unclear, then nothing beats a unit test that can test 1000's of permutations and their expected output.
You really don't even have to go that far as I imagine that you only want to test some special edge cases. Your programming skills and the compiler are your best friend here. The program you write will either confirm or deny your suspicions.
Also, to throw in a bonus you can monitor how long each method takes and see which is more performant.
Just a thought.
回答7:
I'm not sure why you wouldn't just use intval. Your regex isn't even accounting for negative numbers, after all, while intval will (though maybe that's what you're going for?).
Or even just casting to int, which avoids some of the esoteric floating point "gotchas" that can sneak up with intval.
来源:https://stackoverflow.com/questions/462427/is-numeric-intval-ctype-digit-can-you-rely-on-them