使用 ctype_digit($_GET['x']) 和 $_GET['x'] > 0?
Using ctype_digit($_GET['x']) with $_GET['x'] > 0?
像这样在 if statement
中使用 ctype_digit()
和 comparison operator
有什么意义吗
if ($_GET['x'] > 0 && ctype_digit($_GET['x'])) {
echo 'It is a Number';
}
不是真的。来自 the manual(强调我的):
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
因此,如果 x
是 "non numeric string",您的第一次检查将失败并且 short-circuit 条件,使得 ctype_digit()
在这种情况下变得多余。
但是,这种转换要小心。 123abc
例如,您的第一次检查 return 为真(因为为了比较,使用了 123
),所以根据这有多严格,也许可以做一个彻底的检查。
$s = "123abc";
var_dump($s > 0); // true
是的,如果你想确保输入是一个十进制数,并且大于零,这是可能的
像这样在 if statement
中使用 ctype_digit()
和 comparison operator
有什么意义吗
if ($_GET['x'] > 0 && ctype_digit($_GET['x'])) {
echo 'It is a Number';
}
不是真的。来自 the manual(强调我的):
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
因此,如果 x
是 "non numeric string",您的第一次检查将失败并且 short-circuit 条件,使得 ctype_digit()
在这种情况下变得多余。
但是,这种转换要小心。 123abc
例如,您的第一次检查 return 为真(因为为了比较,使用了 123
),所以根据这有多严格,也许可以做一个彻底的检查。
$s = "123abc";
var_dump($s > 0); // true
是的,如果你想确保输入是一个十进制数,并且大于零,这是可能的