A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearIs there a difference between $str == '' and strlen($str) == 0 in PHP? - page 1
User InfoPosts
Is there a difference between $str == '' and strlen($str) == 0 in PHP?#1
As the title says: Is there a difference between $str == '' and strlen($str) == 0 in PHP? Is there any real speed difference and is one better to use than the other?

posted date: 2008-12-19 11:25:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-19 11:25:01


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#3
$str == '' is better practice. There probably isn't much difference in PHP, since it knows the length of its strings, but it'd be a really bad habit to have if you then went and did some work in a lower-level language.

posted date: 2008-12-19 11:29:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#4
The speed difference is too small to matter, I'm sure. The best method to use is the more readable one. Here are two other alternatives:if (empty($str)) { ...}if (!$str) { ...}Note: These will both evaluate to true if $str == '0'.

posted date: 2008-12-19 11:30:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#5
strlen($str) == 0 requires a call to strlen and a call to do the comparison. $str == '' is just a comparison.

posted date: 2008-12-19 11:31:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#6
There might be some type-conversion issues when doing $str==''. For example, consider cases when $str is false (boolean) or 0 (integer). They might just come out equal too. I'm not exactly sure about this, so you should muck around with it a bit. The safe way would be to do $str===''.

posted date: 2008-12-19 11:33:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#7
Those examples aren't correct. empty('0') == empty('') is true

posted date: 2008-12-19 11:35:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#8
Yes, there is an important difference. The == operator does type conversion, so it's not always going to do what you expect. For example, (0 == "") returns true. So you're making an assumption that $str is actually a string. If you're sure this is the case, or if you don't care about the conversions, then it's fine. Otherwise you should use ===, and take steps to ensure that you're comparing strings.

posted date: 2008-12-19 11:36:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#9
The main difference is that $str == '' will return true for anything that's equivalent to the empty string (0 and false, among others).You should use either the === operator to test for an actual empty string or, if you don't mind the convert and test behavior, just use !$str (consider empty() as well, depending on the actual intent), which I find clearer in intent than $x == '' (Did he miss an = sign and actually wants to test for an empty string?.) Use strlen($str) only when you really are after the length of a string. Bottom line, use the proper tool for the job, judging which tool is proper based on the intent of the code.vinko@parrot:~$ cat emptytest.php<?php$a = "";$b = 0;$c = false;$d = "Hi!";function is_empty1($x) { return $x == ''; }function is_empty2($x) { return strlen($x) == 0; }function is_empty3($x) { return $x === ''; }function check_empty($arr) { foreach ($arr as $v) { echo "value is "; var_dump($v); echo " \$x == '' "; var_dump(is_empty1($v)); echo " strlen($x) == 0 "; var_dump(is_empty2($v)); echo " \$x === '' "; var_dump(is_empty3($v)); }}check_empty(array($a,$b,$c,$d));?>vinko@parrot:~$ php emptytest.phpvalue 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)

posted date: 2008-12-19 11:37:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#10
Yes but this is a place where correct answers should be given. Not answers that almost always work in the code you write.

posted date: 2008-12-19 11:38:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#11
I performed a simple benchmark. (I've never done one so this may be completely invalid.) It tests == '', strlen() == 0, === '', and 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";Results:$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 secondsstrlen('') == ''; 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 secondsstrlen('') === ''; took 2.40874910355 secondsAs you can see, comparing to '' is about twice as fast as comparing the length of the string. Also, using === is slightly faster than using ==, and it's type safe! Nice.

posted date: 2008-12-19 11:40:00


Re: Is there a difference between $str == '' and strlen($str) == 0 in PHP?#12
Alright, I added a note about it to my answer.

posted date: 2008-12-19 11:44:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.