is_numeric、intval、ctype__digit..您可以信赖它们吗?

发布于 2024-07-12 08:00:40 字数 191 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

香草可樂 2024-07-19 08:00:40

ctype_digitis_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 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.

a√萤火虫的光℡ 2024-07-19 08:00:40

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);

没企图 2024-07-19 08:00:40

您命名了两种函数:

验证器检查给定值是否具有给定特征并返回truefalse
is_numericctype_* 函数 和您的 isNum 函数是验证函数,因为它们只是告诉您值是否有效。

过滤器会更改给定值,使新值具有给定特征,因此将有效
intvalfilter_* 函数 是过滤函数,因为它们始终会返回有效值这将通过验证器。

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.

执笔绘流年 2024-07-19 08:00:40

您还可以使用新的过滤器函数

if (!$var = filter_var($var, FILTER_VALIDATE_INT)) {
  die('Not an int!');
}

echo "Var has the value $var.\n";

最好在过滤来自 cli、Web 客户端等的输入时使用。此处的过滤器列表< /a>.

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.

把时间冻结 2024-07-19 08:00:40

遵循文档。 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. The ctype_* functions are a little bit narrower in scope but should also always be available.

A RegEx is, IMO, overkill for such checks.

叹倦 2024-07-19 08:00:40

我不是 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.

贪了杯 2024-07-19 08:00:40

我不确定你为什么不只使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文