Php is_数值函数给出了意外的结果
我编写了以下php代码Php is_数值函数给出了意外的结果,php,isnumeric,Php,Isnumeric,我编写了以下php代码 <?php $k ="123e5"; if(is_numeric($k)){ echo "is number"; } else{ echo "is not a number"; } ?> 预期输出为“不是数字”,因为字符串中存在e。但是我得到的输出是数字。为什么会这样?请帮我找到解决办法 根据以下文件: 数字字符串由可选符号、任意位数、, 可选小数部分和可选指数部分。因此+0123.45e6 是有效的数值。十六进制(例如0xf4c3b0
<?php
$k ="123e5";
if(is_numeric($k)){
echo "is number";
}
else{
echo "is not a number";
}
?>
预期输出为“不是数字”,因为字符串中存在e。但是我得到的输出是数字。为什么会这样?请帮我找到解决办法 根据以下文件:
数字字符串由可选符号、任意位数、,
可选小数部分和可选指数部分。因此+0123.45e6
是有效的数值。十六进制(例如0xf4c3b00c)、二进制(例如。
0B101001111001),也允许使用八进制(例如0777)符号,但仅限于
无符号、小数和指数部分
因此,它得到了正确的输出
我想你在找我。然后使用is_int()
。您可以使用ctype_digit()检查数字字符
您应该使用PHP的内置过滤函数来验证用户输入。请看一下以下手册页面:那么e
是一个指数吗?实际上123e5
是一个数字。请参阅文档:如果您想检查数字,可以尝试使用ctype\u digit()
,因为我的问题“e”不是指数。但是我认为is_numeric被视为指数。如果它接受浮点数,例如“123.45”?你能为我的问题提出一个解决方案吗?那么,你想要什么呢?is_int()
对字符串不起作用,它将始终是false
。如果你想使用is_int()对int进行转换,则需要键入casting始终返回true
,这是一个无用的操作:)只是不要忘记
和-
都不是数字。
<?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
?>