NYCPHP Meetup

NYPHP.org

[nycphp-talk] Extracting an int from a query string.

John Campbell jcampbell1 at gmail.com
Tue Nov 13 10:57:50 EST 2007


On Nov 13, 2007 9:59 AM, Brian D. <brian at realm3.com> wrote:
> http://us.php.net/intval
>
> You can use:
> $page_index = intval($_GET['page']);
>
> Returns 0 if it's not a valid integer.
>

I used to do that, but it becomes impossible to distinguish between 0
and null.  Which is okay until you inherit a database with zeros as
keys.
Maybe:
$page_index = isset($_GET['page']) ? intval($_GET['page']) : null;

This is will not produce any strict errors, but if page='hello' then
$page_index is 0.  I would rather it be null in that case.

If php's '||' operator wasn't so stupid, we could use constructs like:
$page_index = intval($_GET['page']) || null;

Auto casting with || and && is on my top 10 list of things I hate about php.

-john campbell



More information about the talk mailing list