is_numeric、intval、ctype__digit .. 你能依赖它们吗? [英] is_numeric, intval, ctype__digit.. can you rely on them?
问题描述
is_numeric、intval、ctype__digit .. 你能依赖它们吗?
还是我必须使用正则表达式?
<前>函数 isNum($str) {return (preg_match("/^[0-9]+$/", $str));
}
大家怎么看?我傻吗?
ctype_digit
和 is_numerict
之间的一个重要区别是负值和浮点数.
is_numeric(-10)
将返回 true
而'ctype_digit(-10)' 将是 false
还有 ctype_digit(12.50)
将返回 false
而is_numeric(12.50)
将是 true
所以它们是否方便取决于您的域逻辑的上下文.
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?
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.
这篇关于is_numeric、intval、ctype__digit .. 你能依赖它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!