Reply to Re: [PHP] shortest possible check: field is set, integer or 0

Your name:

Reply:


Posted by Jochem Maas on 09/30/38 11:33

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

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация