Is there any point to use ctype_digit()
with a comparison operator
in if statement
like this
if ($_GET['x'] > 0 && ctype_digit($_GET['x'])) {
echo 'It is a Number';
}
Is there any point to use ctype_digit()
with a comparison operator
in if statement
like this
if ($_GET['x'] > 0 && ctype_digit($_GET['x'])) {
echo 'It is a Number';
}
Not really. From the manual (emphasis mine):
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.
So if x
is a "non numeric string", your first check will fail and short-circuit the conditions, making ctype_digit()
redundant in this situation.
However, be careful with this conversion. 123abc
for example will return true for your first check (since for the comparison, 123
is used), so depending on how strict this is, maybe do a thorough check instead.
$s = "123abc";
var_dump($s > 0); // true
Yeah, it's possible if you want to make sure the input is a decimal number, and greater than zero