正如标题所说:PHP中的$ str ==''和strlen($ str)== 0之间有区别吗?有没有真正的速度差异,并且比另一个更好用?
我发现只做“ if(!$ str)”就更清楚了..不确定'=='而是'!' 应该能够产生更好的优化技术,因为从来没有进行过类型转换,并且对于字符串,数组,布尔值,数字是安全的...
是的,有一个重要的区别.==运算符进行类型转换,因此并不总是按照预期执行.例如,(0 =="")返回true.所以你假设$ str实际上是一个字符串.如果你确定是这种情况,或者如果你不关心转换,那就没关系.否则你应该使用===,并采取措施确保你正在比较字符串.
主要区别在于$ str ==''将返回true,表示等同于空字符串(0和false 等).
您应该使用===运算符来测试实际的空字符串,或者,如果您不介意转换和测试行为,只需使用!$ str(也可以考虑empty(),具体取决于实际意图),我觉得它的意图比$ x ==''更清楚(他是否错过了一个=符号并且实际上想要测试一个空字符串?)
只有当你真正追求字符串的长度时才使用strlen($ str).
底线,使用适当的工具,根据代码的意图判断哪个工具是正确的.
vinko@parrot:~$ cat emptytest.php vinko@parrot:~$ php emptytest.php value is string(0) "" $x == '' bool(true) strlen() == 0 bool(true) $x === '' bool(true) value is int(0) $x == '' bool(true) strlen() == 0 bool(false) $x === '' bool(false) value is bool(false) $x == '' bool(true) strlen() == 0 bool(true) $x === '' bool(false) value is string(3) "Hi!" $x == '' bool(false) strlen() == 0 bool(false) $x === '' bool(false)
我执行了简单的基准测试.(我从来没有做过,所以这可能完全无效.)它测试=='',strlen()== 0,===''和strlen()=== 0.
header('Content-type: text/plain'); // -- Testing == ''; $string = ''; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) $string == ''; $endTime = microtime(true); echo "\$string = ''; \$string == ''; took " . ($endTime - $startTime) . " seconds\n"; $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) $string == ''; $endTime = microtime(true); echo "\$string = '$string'; \$string == ''; took " . ($endTime - $startTime) . " seconds\n"; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) '' == ''; $endTime = microtime(true); echo "'' == ''; took " . ($endTime - $startTime) . " seconds\n"; // -- Testing strlen() == 0; $string = ''; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen($string) == 0; $endTime = microtime(true); echo "\$string = ''; strlen(\$string) == 0; took " . ($endTime - $startTime) . " seconds\n"; $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen($string) == 0; $endTime = microtime(true); echo "\$string = '$string'; strlen(\$string) == 0; took " . ($endTime - $startTime) . " seconds\n"; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen('') == 0; $endTime = microtime(true); echo "strlen('') == ''; took " . ($endTime - $startTime) . " seconds\n"; // -- Testing === ''; $string = ''; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) $string === ''; $endTime = microtime(true); echo "\$string = ''; \$string === ''; took " . ($endTime - $startTime) . " seconds\n"; $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) $string === ''; $endTime = microtime(true); echo "\$string = '$string'; \$string === ''; took " . ($endTime - $startTime) . " seconds\n"; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) '' === ''; $endTime = microtime(true); echo "'' === ''; took " . ($endTime - $startTime) . " seconds\n"; // -- Testing strlen() === 0; $string = ''; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen($string) === 0; $endTime = microtime(true); echo "\$string = ''; strlen(\$string) === 0; took " . ($endTime - $startTime) . " seconds\n"; $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen($string) === 0; $endTime = microtime(true); echo "\$string = '$string'; strlen(\$string) === 0; took " . ($endTime - $startTime) . " seconds\n"; $startTime = microtime(true); for($i = 0; $i < 10000000; ++$i) strlen('') === 0; $endTime = microtime(true); echo "strlen('') === ''; took " . ($endTime - $startTime) . " seconds\n";
结果:
$string = ''; $string == ''; took 1.01983308792 seconds $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $string == ''; took 1.04193401337 seconds '' == ''; took 1.06608295441 seconds $string = ''; strlen($string) == 0; took 2.1510848999 seconds $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; strlen($string) == 0; took 2.27101397514 seconds strlen('') == ''; took 2.5775551796 seconds $string = ''; $string === ''; took 0.854554176331 seconds $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; $string === ''; took 0.714010000229 seconds '' === ''; took 0.749495983124 seconds $string = ''; strlen($string) === 0; took 1.9263010025 seconds $string = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'; strlen($string) === 0; took 1.95309996605 seconds strlen('') === ''; took 2.40874910355 seconds
如您所见,与''的比较速度大约是比较字符串长度的两倍.另外,使用===比使用==略快,而且它的类型安全!尼斯.