note 34103 deleted from function.abs by hholzgra
| From: | hholzgra@php.net | Date: | Sun, 15 Apr 2012 19:26:11 +0000 |
| Subject: | note 34103 deleted from function.abs by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187139@lists.php.net to get a copy of this message | ||
Note Submitter: bgustin AT trukfixer DOT com
----
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.