0

I often see strlen used. Are these 2 tests equivalent for all values of $str?

is_string($str) && 0 !== strlen($str)

is_string($str) && '' !== $str

ryanve
  • 50,076
  • 30
  • 102
  • 137
  • 2
    `is_string($str) && '' !== $str` is redundant since you are testing with !== that already test thath $str has the same type as ''. – macjohn Dec 02 '11 at 23:01
  • 1
    @macjohn it's not redundant because `strlen(false)` and `strlen(null)` both evaluate to 0. It ensures that `$str` is in fact a string. – ryanve Dec 02 '11 at 23:09
  • @ryanve @macjohn is saying that the `is_string($str)` part of the _second_ test is redundant. – Matt Ball Dec 02 '11 at 23:19
  • @MДΓΓ БДLL It's not redundant in either case. Consider that `$str` may be a non-string type like an array or boolean. – ryanve Dec 02 '11 at 23:21
  • 1
    in the first case, if $str is not a string, and you don't test if $str is a string, strlen will throw an error, since it expects a string as a parameter. In the second case the !== operator test both the type and the content. – macjohn Dec 02 '11 at 23:29
  • @macjohn What would be the sense in doing it in a way that might throw an error? That's wrong anyway. Try `echo strlen(array());`. It's 5 b/c it casts the array to 'Array'. – ryanve Dec 03 '11 at 01:11
  • Think that conceptually when you do a test with $a !== $b you are doing a double test: gettype($a) !== gettype($b) && $a != $b. In your example gettype('') !== 'string' && '' != $str – macjohn Dec 03 '11 at 08:04

4 Answers4

3

Yes, those two statements are logically equivalent. My preferred ways to skin this cat:

is_string($str) && !empty($str)

...though empty('0') is true (sigh, PHP...), so this is probably even better:

$str !== ''

See also: Checking if the string is empty and Is there a difference between $str == '' and strlen($str) == 0 in PHP?

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

They're pretty close, except that strlen() will return 0 for NULL strings, so if your $str was NULL, the 0 !== strlen($str) expression in your first test would evaluate to true, while the '' !== $str in your second test would evaluate to false.

Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
1

yes, they are the same..

I would use:

is_string($str) && strlen($str) > 0
Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80
  • Thx - If they're the same then why use the one that requires an extra function call? I thought maybe it was faster to use `strlen` but I tested it performance-wise and they were so close that it was hard to tell which was faster. – ryanve Dec 02 '11 at 23:19
-1

Try this

strlen($str) > 0

!== is a strong type and might not be match the 0 properly.

Sean H Jenkins
  • 1,770
  • 3
  • 21
  • 29
  • I prefer to check if `$str` is a actually string as well. – Wouter Dorgelo Dec 02 '11 at 23:08
  • @Pallazzo Yea you definitely have to make sure it's a string. I figured that's what Sean meant. – ryanve Dec 02 '11 at 23:14
  • 1
    strlen() behaved stupid until 5.3 if fed an array as argument. You would get length 5 because array typecasts to the string 'Array'. So this isn't an ideal way to kill 2 birds with one stone; you still need to verify it was a string. – goat Dec 02 '11 at 23:16
  • Yes I understand you may want to test for a string, but thats pretty straightforward. Whereas using !== can often break things – Sean H Jenkins Dec 02 '11 at 23:18
  • @Sean H Jenkins I see how `!=` might break things but `!==` seems fine. What do you mean? – ryanve Dec 03 '11 at 01:16
  • For example if you have (int) 5 === (float) 5 = false But (int)5 == (float) 5 = true – Sean H Jenkins Dec 03 '11 at 10:28