NYCPHP Meetup

NYPHP.org

[nycphp-talk] ctype_digit in PHP 5.1.x

Hans Zaunere lists at zaunere.com
Fri Dec 2 12:17:41 EST 2005



Scott Mattocks wrote on Friday, December 02, 2005 11:33 AM:
> 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.

Script and output...

$TheString = '';

echo 'Empty String: ';
var_dump(ctype_digit($TheString));

$TheString = NULL;

echo 'NULL: ';
var_dump(ctype_digit($TheString));


----Output

Empty String: bool(false)
NULL: bool(false)


> > 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.

So yeah, it appears to be as expected now.  However, ctype_digit() is really
just a wrapper around the native C functions, so the setlocale() and family
of libs are something to consider.  The above script I did produces the same
output on Fedora and Windows, but man setlocale and man isdigit could also
cover it.


---
Hans Zaunere / President / New York PHP
   www.nyphp.org  /  www.nyphp.com






More information about the talk mailing list