Page 1 of 1

Form input variable type interpretation problem

Posted: Thu Oct 30, 2008 2:10 pm
by Hairy Potter
Hi. In the script processing a form I included conditional statement:

if((is_int($tireqty)==FALSE)||(is_int($oilqty)==FALSE)||(is_int($sparkqty)==FALSE)){
echo '<h1><b><font color=red>Wrong data inputted! Quantities have to be integers! </font></b></h1>';
exit;
}

where $tireqty, $oilqty and $soarkqty are input variables. However is_int() returns FALSE every time, because whatever data is inputted using the form, it is always interpreted as string type, which I checked using gettype() function. Why is that? Please help.

Re: Form input variable type interpretation problem

Posted: Thu Oct 30, 2008 3:32 pm
by requinix
Data coming from outside (like through $_GET or $_POST) will always be a string.

The string equivalent to is_int is ctype_digit: checks if a string has only digits (no letters, not even a negative sign or decimal point). If you want to know if something is a number (could be an integer or a real) then use is_numeric.

Re: Form input variable type interpretation problem

Posted: Thu Oct 30, 2008 3:57 pm
by Hairy Potter
Thanks a lot. ctype_digit is the function I needed.