NYCPHP Meetup

NYPHP.org

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

John Campbell jcampbell1 at gmail.com
Tue Nov 13 15:14:47 EST 2007


On Nov 13, 2007 2:56 PM, dann <dann at bentobox.net> wrote:
> One small caveat I failed to mention earlier: while ctype_digit will
> work fine for parsing the superglobals, if you are instead parsing an
> unknown variable that might be an actual integer, a string containing
> an integer, or a non-integer string, you'll need to first cast it to
> a string before you apply ctype_digit, as that function returns false
> if you pass it an actual integer. It's an easy "feature" to forget,
> and as I did exactly that myself a few minutes ago I thought I'd
> mention it to potentially save you some time in the future.
>
> $string = '42';
> $int = 42;
> var_dump(ctype_digit($string), ctype_digit((string) $int), ctype_digit
> ($int));

Easily fixed with:
ctype_digit("$int");
With the added bonus, that another dev will come behind you, remove
the quotes, and create a bug. :)



More information about the talk mailing list