is_numeric、intval、ctype__digit..您可以信赖它们吗?
is_numeric、intval、ctype__digit..您可以信赖它们吗?
或者我必须使用正则表达式?
function isNum($str) {
return (preg_match("/^[0-9]+$/", $str));
}
你们有什么感想? 我是傻子吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ctype_digit
和is_numerict
之间的一个重要区别是负值和浮点数。is_numeric(-10)
将返回true
而'ctype_digit(-10)' 将为
false
并且
ctype_digit(12.50)
将返回false
而is_numeric(12.50)
将为true
因此,它们两者是否方便取决于您的域逻辑的上下文。
One important difference between
ctype_digit
andis_numerict
is negative values and float number.is_numeric(-10)
will returntrue
whereas'ctype_digit(-10)' will be
false
also
ctype_digit(12.50)
will returnfalse
whereasis_numeric(12.50)
will betrue
So both of them are handy depends on the context of your domain logic.
is_numeric、intval 和 ctype_digit 都执行非常不同的操作。
is_numeric 会告诉你变量的内容是否是数字(即,如果它是浮点或整数值,则为 true)。
intval 尝试将数字字符串转换为整数值
ctype_digit 会告诉您字符串是否仅包含数字字符(将执行与 isNum 函数相同的检查)。
最好的方法可能是检查 is_numeric 是否为 true,然后使用类似于 settype($myvalue, 'integer') 或 intval($myvalue); 的内容;
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);
您命名了两种函数:
验证器检查给定值是否具有给定特征并返回true或false。
is_numeric
,ctype_*
函数 和您的isNum 函数是验证函数,因为它们只是告诉您值是否有效。
过滤器会更改给定值,使新值具有给定特征,因此将有效。
intval
和filter_*
函数 是过滤函数,因为它们始终会返回有效值这将通过验证器。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
, thectype_*
functions and yourisNum
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 thefilter_*
functions are filtering functions as they always will return valid values that would pass a validator.您还可以使用新的过滤器函数。
最好在过滤来自 cli、Web 客户端等的输入时使用。此处的过滤器列表< /a>.
You can also use the new filter functions.
Best used when filtering input from cli, web client, etc. List of filters here.
遵循文档。
is_numeric
将始终可用,并且仅验证您是否拥有可以被 PHP 视为数字的字符串。ctype_*
函数的范围稍微窄一些,但也应该始终可用。在我看来,正则表达式对于此类检查来说太过分了。
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. Thectype_*
functions are a little bit narrower in scope but should also always be available.A RegEx is, IMO, overkill for such checks.
我不是 PHP 专家,但是您是否考虑过编写数据驱动的单元测试来具体了解这些函数。 如果您不确定它们的可靠性并且文档也不清楚,那么没有什么比可以测试 1000 个排列及其预期输出的单元测试更好的了。
您实际上甚至不必像我想象的那样走得那么远,您只想测试一些特殊的边缘情况。 您的编程技能和编译器是您最好的朋友。 您编写的程序将证实或否认您的怀疑。
此外,为了增加奖励,您可以监控每种方法需要多长时间,看看哪种方法性能更高。
只是一个想法。
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.
我不确定你为什么不只使用 intval。 毕竟,您的正则表达式甚至不考虑负数,而 intval 会(尽管也许这就是您想要的?)。
或者甚至只是强制转换为 int,这可以避免一些可能在 intval 中出现的深奥浮点“陷阱”。
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.