note 34103 modified in function.abs by vrana

From: Date: Tue, 09 Dec 2003 19:56:39 +0000
Subject: note 34103 modified in function.abs by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-61700@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... <?php 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. --was-- 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. http://www.php.net/manual/en/function.abs.php

« previous php.notes (#61700) next »