Re: ctype second opinions
| From: | Brent Cook | Date: | Fri, 19 Jul 2002 14:23:49 +0000 |
| Subject: | Re: ctype second opinions | ||
| References: | 1 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-7785@lists.php.net to get a copy of this message | ||
On Fri, 19 Jul 2002, Pierre-Alain Joye wrote:
> On Fri, 19 Jul 2002 08:41:53 -0500 (CDT)
> Brent Cook <busterb@mail.utexas.edu> wrote:
>
> > Dear all,
> >
> > I would like your opinion, please. I'm currently arguing a bug in the
> > PHP database, http://bugs.php.net/bug.php?id=18428, and
> > basically feel
> > that I have made the correct choice, while the PHP developer feels
> > otherwise.
> >
> > The ctype functions in PHP currently return TRUE on an empty string. For
> > instance, ctype_alpha('a') == TRUE, ctype_alpha('1') == FALSE,
> > ctype_alpha('') == TRUE. I argued that since no characters in an empty
> > string match the criteria, an empty string should return FALSE. The
> > developer argued that "in an empty string every of the 0 charactetrs
> > matches the (criteria)", so it should return TRUE.
>
> afaik, ctype_alpha checks only the type, returning true if the param is
> an alhatype. An empty string is an alpha. That s a fact.
Yes, that is what it does currently. What about:
ctype_space(''), ctype_print('') or ctype_cntrl('')
these also return true. Does an empty string intuitively contain spaces,
printable characters or control characters?
> > if (strlen($c) && ctype_alpha($c))
>
> can be replaced by :
> if ( ctype_alpha($c) && strlen($c) )
>
> You can check the string length only if $c is a string.
In this case, I know that the argument is a string; rather this ordering
takes advantage of short-circuit logic to avoid calling ctype_isalpha() if
the string is zero length.
The whole of the logic that I am trying to convey here similar to:
$string = 'This is a string';
$i = 0;
while ($string[$i] != '') {
$token = '';
while (strlen($string[$i]) && ctype_alpha($string[$i]) {
$token .= $string[$i];
++$i;
}
$tokens[] = $token;
++$i;
}
Without checking strlen, this snippet will go into an infinite loop when
it reaches the end. And don't say to use strtok(), I'm doing more than
just this ;)
- Brent