Re: Ensuring that variable contains a number

From: Date: Sat, 09 Dec 2006 22:38:32 +0000
Subject: Re: Ensuring that variable contains a number
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-245674@lists.php.net to get a copy of this message
My take: $number = (int) $number; if ($number < 1) $number = 1; You could error out for < 1, or compare the original with the (int) cast to see if PHP considers them = and error out if not, while using the (int) cast to be SURE you have an int, or... But a typecast and inequality test should be pretty clear and cheap. On Wed, December 6, 2006 2:01 pm, Dotan Cohen wrote: > 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? > > Dotan Cohen > > http://what-is-what.com/what_is/internet.html > http://song-lirics.com/ > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some starving artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?

« previous php.general (#245674) next »