2 ms·
Well, if you prefer it that way, there is a 'null' keyword as well that does exactly what you want: $str = string_function(...); if ( is_null( $str ) )
by nuclear_eclipse 18y ago
Well, if you prefer it that way, there is a 'null' keyword as well that does exactly what you want:
$str = string_function(...);
if ( is_null( $str ) ) {}
if ( null === $str ) {}
Just a matter of preference.
- ajross 18y agoTo be clear: it's a matter of correctness, not preference. Using a boolean constant to represent "not a string" when there are legal string values that are equal to that constant is a design bug, that you are working around via the identity operator. I'm happy that PHP has a null; less so that you don't see value in using it where it's required.
- nuclear_eclipse 18y agoWell, my point is: $str = ''; $str == null; #true $str == false; #true null == false; #true So in the case of PHP, it's not a matter of correctness, simply because of how PHP handles type coercion. 'false' and 'null' both have legal string values that can evaluate as equal to them or each other.