[thelist] PHP question... checking string length

Andrew Clover and-evolt at doxdesk.com
Sat Mar 26 21:44:15 CST 2005


Theodore Serbinski <stanson at gmail.com> wrote:

> But I recently stumbled upon some code from a much more experienced
> PHP person test empty strings like so:

> if (strlen($myString) == 0) // do something

> Is there any difference/advantage to using one over the other?

Your version is more readable IMO.

Testing len==0 is a common Java optimisation, as apparently it is 
quicker to do a .length() check than a full string comparison. In Java, 
all Strings, even string literals, are mutable and so comparisons have 
to be done using a.equals(b) rather than == (a common source of bugs and 
a horrible design decision IMO), so the .length() test is perhaps not so 
much less readable in this case.

Whether there is any performance advantage in PHP, I don't know.

-- 
Andrew Clover
mailto:and at doxdesk.com
http://www.doxdesk.com/


More information about the thelist mailing list