Bug in PHP's ctype_digit()

I recently encountered a bug in PHP's function ctype_digit() (available since PHP 4.0.4). The function checks that a string passed to it consists entirely of digits, so is often used to verify that a variable can be treated as an integer.

The bug is that if you call ctype_digit() and pass an integer to it, it sets the integer passed to NULL. Curiously, when I try passing an integer in the range -128 to 255, it is not affected by the bug. The way to avoid the bug happening at all is to pass the function a string. For example, instead of calling ctype_digit($myInt), call ctype_digit("$myInt").

The bug is apparent in the two versions of PHP I'm using, 4.3.10 and 4.3.11. I'm not sure from this bug report if any versions of PHP 4 have had this fixed. Provided you have a version of PHP that has not had this error fixed, the bug can be seen by running the following PHP snippet:

<?php $i = 99999; var_dump($i); ctype_digit($i); var_dump($i); ?>


int(99999) NULL

As mentioned, embedding the integer in a string solves the problem:

<?php $i = 99999; var_dump($i); ctype_digit("$i"); var_dump($i); ?>


int(99999) int(99999)