Form input variable type interpretation problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Hairy Potter
Forum Newbie
Posts: 2
Joined: Thu Oct 30, 2008 1:24 pm

Form input variable type interpretation problem

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Form input variable type interpretation problem

Post 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.
Hairy Potter
Forum Newbie
Posts: 2
Joined: Thu Oct 30, 2008 1:24 pm

Re: Form input variable type interpretation problem

Post by Hairy Potter »

Thanks a lot. ctype_digit is the function I needed.
Post Reply