Re: Ensuring that variable contains a number

From: Date: Thu, 07 Dec 2006 16:18:53 +0000
Subject: Re: Ensuring that variable contains a number
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-245614@lists.php.net to get a copy of this message
# dotancohen@gmail.com / 2006-12-06 23:51:45 +0200: > >> 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? > Thanks. As the 'number' is used in a for loop, it actually is alright > if it is a float. I just added a check that the number is indeed >=1, > otherwise I set it to 1. Good enough for this project, anyway... :) $number = max(intval($number), 1); -- How many Vietnam vets does it take to screw in a light bulb? You don't know, man. You don't KNOW. Cause you weren't THERE. http://bash.org/?255991

« previous php.general (#245614) next »