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?