
Originally Posted by
CobraCom
Why use strlen to check if the string is 0 chars? just checking if it's not empty reduces the server load time.
Using strlen($string) is the same as using string.length - which is somewhat of a standard in checking if a string is empty or not.
For example, if(string.length>0) grant access to this script;
I tend to use cross compatible tactics because it works in every language, maybe a bit differently- and there's no difference in performance that I know of.
Here's a little test for ya,
PHP Test Code:
PHP Code:
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return substr(((float)$usec + (float)$sec),0,15);
}
$str_1000 = (string) '1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111';
$str_1 = (string) null;
$int_0 = (int) 0;
$start = microtime_float();
$test = isset($int_0);
$end = microtime_float();
echo '<p>Int 0: '.$int_0.'
<br />
Is Set: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = isset($str_1);
$end = microtime_float();
echo '<p>String 1: '.$str_1.'
<br />
Is Set: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = isset($str_1000);
$end = microtime_float();
echo '<p>String 1000: '.$str_1000.'
<br />
Is Set: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = strlen($int_0);
$end = microtime_float();
echo '<p>Int 0: '.$int_0.'
<br />
String Length: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.'<br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = strlen($str_1);
$end = microtime_float();
echo '<p>String 1: '.$str_1.'
<br />
String Length: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.'<br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = strlen($str_1000);
$end = microtime_float();
echo '<p>String 1000: '.$str_1000.'
<br />
String Length: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.'<br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = empty($int_0);
$end = microtime_float();
echo '<p>Int 0: '.$int_0.'
<br />
Empty: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = empty($str_1);
$end = microtime_float();
echo '<p>String 1: '.$str_1.'
<br />
Empty: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
$start = microtime_float();
$test = empty($str_1000);
$end = microtime_float();
echo '<p>String 1000: '.$str_1000.'
<br />
Empty: '.$test.'<br />
Start: '.$start.'<br />
End: '.$end.' <br />
Duration: '.substr(($end-$start),0,10).'</p>
';
?>
My Result: [Results may vary, of course ;)] Normally it's 0 for all of them, so they all perform the same, no matter if there's 1000 characters or 0. I even tried it with 100k! Note that I'm not logging the time it takes to set the variable; which does of course effect load time.
With this test: I'm using 3 different predefined variables under 3 different methods.
You should always predefine variables. Therefore: Don't use isset() to check existing variables.
*Does not recognize null as empty.
Isset returns 1 for null.
isset returns 1 for 0
isset returns 1 for 1k 1's.
Checks String Length for ints/strings.
*Recognizes null as empty.
strlen returns 0 for null.
strlen returns 1 for 0.
strlen returns 1000 for 1k 1's.
Works for strings, works for ints, but returns empty if the int is 0.
*Recognizes null as empty.
empty returns 1 for null.
empty returns 1 for 0.
empty returns null for 1k 1's.
I see nothing against calling empty() the best solution.
It's just habit for me to use strlen($str), & str.length;there is no disadvantage imo..
Code:
<p>Int 0: 0
<br />
Is Set: 1<br />
Start: 1243286735.2258<br />
End: 1243286735.2258 <br />
Duration: 0</p>
<p>String 1:
<br />
Is Set: 1<br />
Start: 1243286735.2259<br />
End: 1243286735.2259 <br />
Duration: 0</p>
<p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
<br />
Is Set: 1<br />
Start: 1243286735.2259<br />
End: 1243286735.2259 <br />
Duration: 0</p>
<p>Int 0: 0
<br />
String Length: 1<br />
Start: 1243286735.226<br />
End: 1243286735.226<br />
Duration: 0</p>
<p>String 1:
<br />
String Length: 0<br />
Start: 1243286735.226<br />
End: 1243286735.2261<br />
Duration: 9.98973846</p>
<p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
<br />
String Length: 1000<br />
Start: 1243286735.2261<br />
End: 1243286735.2261<br />
Duration: 0</p>
<p>Int 0: 0
<br />
Empty: 1<br />
Start: 1243286735.2262<br />
End: 1243286735.2262 <br />
Duration: 0</p>
<p>String 1:
<br />
Empty: 1<br />
Start: 1243286735.2262<br />
End: 1243286735.2262 <br />
Duration: 0</p>
<p>String 1000: 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
<br />
Empty: <br />
Start: 1243286735.2263<br />
End: 1243286735.2263 <br />
Duration: 0</p>
I do agree with the else if() point, that's very true.