Dainis Abols Dainis Abols - 5 months ago 18
PHP Question

If returning wrong result

Not sure if I'm not seeing something or what, but I have a simple

id
validation

$id = '2s2';

var_dump($id);
var_dump($id*1);
if ($id != ($id*1) || $id <= 0) {
die('wrong id');
}
die('here?');


The result:

string '2s2' (length=3)
int 2
here?


It checks if
id
is the same as
id multiplied by 1
or if the value is zero or lower. Since, the value of
$id = '2s2;
it is not the same ( as can be seen in the dump ).

Why is this not echoing
wrong id
?

Answer

I'm guessing you multiply it by 1 to see if the $id is numeric. In that case you could simply:

if (!is_numeric($id) || $id <= 0) {
    die('wrong id');
}