note 34103 added to function.abs

From: Date: Wed, 16 Jul 2003 13:50:22 +0000
Subject: note 34103 added to function.abs
Groups: php.notes 
Request: Send a blank email to php-notes+get-52191@lists.php.net to get a copy of this message
assume we take user input from a form untreated and assign it to variable $range. We want to be sure this number is a Positive Whole number, since abs() just sets a number to positive or 0, and we dont want decimals... if(!is_numeric($range))//checks for numeric input { $range=1; }//sets $range to integer 1 if input is anything other than a numeric value if(!int($range))//checks to make sure it is an integer (not decimal) { $range=int_val($range); }//if a decimal- sets $range to integer value $range=abs($range);//sets value to positive whole number for example the input "testing" would return $range =1, the input "3.578" would return value=3 If the input is null, I am havent tested to see if it gets set to 1 courtesy of int_val or not, but I believe it will be. I'm sure there's probably a more elegant way to do this using regex, however for an apprentice php coder, this might be a little easier to understand and use. ---- Manual Page -- http://www.php.net/manual/en/function.abs.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34103 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34103&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34103&report=yes

« previous php.notes (#52191) next »