Re: Is $a an interger?

From: Date: Tue, 07 Nov 2000 19:31:56 +0000
Subject: Re: Is $a an interger?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-24166@lists.php.net to get a copy of this message
First: PHP thinks values submitted by formular are strings because they REALY are! All you fill in a formular is submitted as string. But u can verify this with JavaScript. See example below: ------------------------------------------------------- <SCRIPT language="Javascript"> function fyIfInteger(theFormElement){ // Receives a string and verify if there is any non-digit character // using Regular Expressions var aux = theFormElement.value; if (aux.search(/[^0-9]/) != -1) { // matches any non-digit character theFormElemen.value = ''; // cleans the field. theFormElemen.focus(); // set the focus ! } } </SCRIPT> ----------------------------------------------------- You call the function with the onBlur event of the text field, passing itself as parameter, see: ----------------------------------------------------- <INPUT TYPE="text" NAME="MyTextField" SIZE=40 VALUE="" onBlur="VerifyIfInteger(this)"> ----------------------------------------------------- Second: PHP is DYNAMIC TYPED. You don't need change any type. you can just do, for example: $a = "10"; $b = 10; $b = $a + $b; echo "$b"; The output will be 20 []'s Leopoldo Wagner p.s.: Sorry by bad englich! :) > Hi folks, this probably qualifies as a dumb newbie question. > > I want to use PHP to make sure a value submitted in a form is an integer, if > not I want to prompt the user to change that value. > > However I have discovered that no matter what I do PHP thinks values > submitted from the form are strings. I tired using settype() but that > changes the value to 0, and when the form is resubmitted the type changes > again to string. > > My brilliant (but non functional code) is\ > $type=gettype($var1); > if ($type=="integer") > > ... > } else { > > if ($type="string") { > settype($var1, "integer") > ...//stuff to show form again > } > } > > What do I need to do to make PHP thin k the values submitted by the form are > integers > > Thanks, > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > >

« previous php.general (#24166) next »