NYCPHP Meetup

NYPHP.org

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

dann dann at bentobox.net
Tue Nov 13 14:56:56 EST 2007


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


>> $page_index = ctype_digit($_GET['page']) ? $_GET['page'] :  
>> $default_page;
>
> Thanks Dann,
> I will start using that from now on.  Easy to read, works with 0.



More information about the talk mailing list