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

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

 



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 :-)

--
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