NYCPHP Meetup

NYPHP.org

[nycphp-talk] ctype_digit in PHP 5.1.x

csnyder chsnyder at gmail.com
Fri Dec 2 12:11:05 EST 2005


On 12/2/05, Scott Mattocks <scott at crisscott.com> wrote:
> Chris Shiflett wrote:
> > Scott Mattocks wrote:
> >> Anyway, the problem is with ctype_digit returning true for null
> >> strings.
> >
> > There is no such thing as a null string, because null is not a string. I
> > think you mean the empty string, based on your example:
>
> Yes. I meant empty string. Sorry.
>
> > Why would this be considered a bug?
> >
> > The ctype_digit() function returns true if every character in a string
> > is a digit. The length of the string seems like a separate criterion.
>
> It would be a bug because it doesn't function as one would expect. The
> manual says, " Returns TRUE if every character in text is a decimal
> digit, FALSE otherwise." Since there are no characters, they cannot be
> decimal digits. Therefore, I would expect the "FALSE otherwise" to kick
> in. If it said, "Returns FALSE if the string contains non-decimal
> characters" I might agree with you. It may just be semantics, but do you
> really expect ctype_digit('') to return TRUE?
>
> Anyway, I decided not to be such a dummy and just downloaded the Windows
> version. In 5.1.1 (on Windows at least), ctype_digit('') returns false.
>
> Thanks,
> Scott


As a non-c programmer (and therefore someone without expectations
about how such a function should work) I have to agree that this seems
like a bug to me, too, and it's one of the main reasons why I don't
use the ctype functions for validation.

An empty string is not a digit.

Nevertheless, it is well documented that the ctype functions return
TRUE on empty values, so if you're going to use them you have to
expect it.

Not actually a bug, but a brain-dead feature.

--
Chris Snyder
http://chxo.com/


More information about the talk mailing list