ctype_digit
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
ctype_digit - 检查数字字符(s)
描述
bool ctype_digit ( string $text )
检查提供的字符串中的所有字符text
是否都是数字。
参数
text
被测试的字符串。
返回值
如果字符串文本中的每个字符都是十进制数字,则返回TRUE,否则返回FALSE。
更新日志
版本 | 描述 |
---|---|
5.1.0 | 在PHP 5.1.0之前,当文本为空字符串时,此函数返回TRUE。 |
例子
示例#1 一个ctype_digit()示例
<?php
$strings?=?array('1820.20',?'10002',?'wsl!12');
foreach?($strings?as?$testcase)?{
????if?(ctype_digit($testcase))?{
????????echo?"The?string?$testcase?consists?of?all?digits.\n";
????}?else?{
????????echo?"The?string?$testcase?does?not?consist?of?all?digits.\n";
????}
}
?>
上面的例子将输出:
The string 1820.20 does not consist of all digits.
The string 10002 consists of all digits.
The string wsl!12 does not consist of all digits.
示例#2 一个ctype_digit()示例,比较字符串与整数
<?php
$numeric_string?=?'42';
$integer????????=?42;
ctype_digit($numeric_string);??//?true
ctype_digit($integer);?????????//?false?(ASCII?42?is?the?*?character)
is_numeric($numeric_string);???//?true
is_numeric($integer);??????????//?true
?>
注意
注意:这个函数需要一个字符串是有用的,所以例如传入一个整数可能不会返回预期的结果。但是,还要注意HTML表单将导致数字字符串而不是整数。另请参阅手册的类型部分。
注意:如果提供了-128到255之间的整数,则它将被解释为单个字符的ASCII值(为了允许扩展ASCII范围中的字符,负值将被添加256)。任何其他整数都被解释为包含整数的十进制数字的字符串。
扩展内容
- ctype_alnum() - 检查字母数字字符(s)
- ctype_xdigit() - 检查代表十六进制数字的字符
- is_numeric() - 查找变量是数字还是数字字符串
- is_int() - 查找变量的类型是否是整数
- is_string() - 查找变量的类型是否是字符串
← ctype_cntrl
ctype_graph →
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com