Re: shortest possible check: field is set, integer or 0

From: Date: Thu, 01 Dec 2005 16:30:38 +0000
Subject: Re: shortest possible check: field is set, integer or 0
References: 1 2 3 4 5  Groups: php.general 
Request: Send a blank email to php-general+get-226712@lists.php.net to get a copy of this message
Steve Edberg wrote:
Only problem with intval() is that it returns 0 (a valid value) on
I knew that. :-)
failure, so we need to check for 0 first. Adding more secure checks
do we? given that FALSE casts to 0.
would make this more than just a one-liner, eg; $_CLEAN['x'] = false; if (isset($_POST['x'])) { if (0 == 1*$_POST['x']) {
I find the 1*_POST['x'] line a bit odd. why do you bother with the '1*' ?
      $_CLEAN['x'] = 0;
} else {
      $x = intval($_POST['x']);
      if ($x > 0 && $x == 1*$_POST['x']) {
this is wrong ... if $_POST['x'] is '5.5' this won't fly but is valid according to the OP.
         $_CLEAN['x'] = $x;
      }
} } Reducing to a two-liner, if you *really* want: $x = intval(@$_POST['x']); $_CLEAN['x'] = (isset($_POST['x']) ? ((0 == 1*$_POST['x']) ? 0 : (($x > 0 && $x == 1*$_POST['x']) ? $x : false)) : false); (all untested) That *should* return false unless all your conditions are set, in which case it will return your cardinal number (non-negative integer). Disclaimer: Currently operating on caffeine deficit; it's possible I'm answering a question no one asked.
now that is funny :-)

« previous php.general (#226712) next »