php - is_int() 和 ctype_digit() 有区别吗?

标签 php function numbers

一个更受欢迎,还是比另一个表现更好?

最佳答案

is_int()如果参数是整数类型,则返回 true,ctype_digit()接受一个字符串参数,如果字符串中的所有字符都是数字,则返回 true。

示例:

┌──────────┬───────────┬────────────────┐
│          │  is_int:  │  ctype_digit:  │
├──────────┼───────────┼────────────────┤
│ 123      │  true     │  false         │
├──────────┼───────────┼────────────────┤
│ 12.3     │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ "123"    │  false    │  true          │
├──────────┼───────────┼────────────────┤
│ "12.3"   │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ "-1"     │  false    │  false         │
├──────────┼───────────┼────────────────┤
│ -1       │  true     │  false         │
└──────────┴───────────┴────────────────┘

关于php - is_int() 和 ctype_digit() 有区别吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/236406/

相关文章:

math - 在Dart中使用parseInt时服务器崩溃?

javascript - 正则表达式比较两个数字

Javascript - RegExp 匹配任何数字,包括无穷大、负数和 e+/- 数字

php - 在 Codeigniter4 中找不到类 'App\Model\Users'

php javascript url编码

php - 如何在 Typo3 9 中检查应用程序上下文?

PHP:正则表达式与 ctype_*

arrays - Swift 乘法数组索引

具有可选第一个参数的 Javascript 函数,但传递了第二个参数

c - C 中的结构函数