RE: Ensuring that variable contains a number

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I think that will only check for a digit value of some kind.  It's not
going to check that $input is positive or if it is an integer.  For
instance, it could be a negative float value as long as $input is
interpreted as a float and not a string. The period would throw it off
it it's a string.  I'd have to test, but anyway you could do something
like this:

<?php
function isPositiveInteger($input) {
$integer = floor($input);
$decimal = $input - $integer;
$returnVal = true;

if ($input < 0) {
	$returnVal = false;
}

if ($decimal != 0) {
	$returnVal = false;
}

return $returnVal;

}

?>

That should work.  There might be a bug in there, but I'll leave that up
to people to test it ;)

--
Ray Hauge
Application Development Lead
American Student Loan Services
www.americanstudentloan.com

-----Original Message-----
From: Brad Fuller [mailto:bfuller@xxxxxxxxxxxxxxxx] 
Sent: Wednesday, December 06, 2006 2:20 PM
To: 'php php'
Subject: RE:  Ensuring that variable contains a number

> How can I ensure that a variable contains a postive integer number?
> I'm currently using this code:
> 
> $number=(10000-$number);
> $number=(10000-$number);
> if ( $number<1 ) { $number=1; }
> 
> But I'm sure that there is a better way. What would that be?


this will check that $input is a positive integer.

if(preg_match("/^\d+$/", $input)) {
	// OK
} else {
	// NOT OK
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux