is_numeric、intval、ctype__digit .. 你能依赖它们吗? [英] is_numeric, intval, ctype__digit.. can you rely on them?

查看:63
本文介绍了is_numeric、intval、ctype__digit .. 你能依赖它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

is_numeric、intval、ctype__digit .. 你能依赖它们吗?

还是我必须使用正则表达式?

<前>函数 isNum($str) {
return (preg_match("/^[0-9]+$/", $str));
}

大家怎么看?我傻吗?

解决方案

ctype_digitis_numerict 之间的一个重要区别是负值和浮点数.

is_numeric(-10) 将返回 true 而'ctype_digit(-10)' 将是 false

还有 ctype_digit(12.50) 将返回 falseis_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆