Re: int and strings

From: Date: Thu, 12 Oct 2000 16:22:21 +0000
Subject: Re: int and strings
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-19844@lists.php.net to get a copy of this message
Hey Bryan: > Hi. I know that when forms get submitted, all the > fields are strings. Suppose I have an age field and I > want to check to see if somebody is over 13, I have > ... > Now, do I need to covert $age to int first or does PHP > automatically do this. Yes, it does it automatically. I'd run your code differently, though: # Set value of age for test purposes... $age = "14"; // $age = "aa14a"; // $age = "2"; # Rip out non numeric characters to ensure user input is valid. $age = ereg_replace("[^0-9]", "", $age); # PHP's automatic type conversion means you don't need is_int() test. # So, go straight to age comparison. if ("$age" <= 13) { echo "<br />$age is 13 or younger"; } else { echo "<br />$age is over 13"; } Enjoy, --Dan -- T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y More than just answers. Solutions. (SM) http://www.analysisandsolutions.com/ 4015 7 Av #4, Brooklyn NY 11232 v: 718-854-0335 f: 718-854-0409

« previous php.general (#19844) next »