[thelist] PHP question... checking string length

Matt Warden mwarden at gmail.com
Fri Mar 25 11:53:27 CST 2005


On Fri, 25 Mar 2005 12:30:58 -0500, Theodore Serbinski
<stanson at gmail.com> wrote:
> Guys, got a quick question. In PHP, I usually test if a string is empty like so:
> if ($myString=='') // do something
> 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? Only
> apparently one is using the first method should be marginally faster
> (no function call). Thanks!

Which one is the faster is questionable. Yes, the latter includes a
function call, but this function call is built in to PHP.

Your solution creates an extra string. Depending on how PHP handles
this, it could be slower or faster.

It's just a style issue, though. Any difference in execution time is
not even worth mentioning. HOWEVER, if you want to test it:

// store current time
while ( (strlen($myString) == 0) && i < 5000) i++;
// find time difference

// store current time
while ( ($myString == '') && $i < 5000) $i++;
// find time difference

Matt Warden
Miami University
Oxford, OH, USA

This email proudly and graciously contributes to entropy.

More information about the thelist mailing list