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
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
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?
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.
yes, they are the same..
I would use:
is_string($str) && strlen($str) > 0
Try this
strlen($str) > 0
!== is a strong type and might not be match the 0 properly.