Hacker News new | ask | show | jobs
by arenaninja 4102 days ago
OH man I want that Null coalesce operator so bad...
1 comments

I feel like I've needed that since I first started coding PHP 17 years ago!
I feel like I want to test what happens if you put values like "", false, " ", 0, an empty array, etc etc etc in front of ??.

Because, you know, this is PHP.

> I feel like I want to test what happens if you put values like "", false, " ", 0, an empty array, etc etc etc in front of ??.

All of those would result in the variable being assigned those values: the null check uses the same semantics as is_null(), and the truth table for those values is:

    $v     is_null($v)
    ------------------
    ''     FALSE
    false  FALSE
    ' '    FALSE
    0      FALSE
    []     FALSE
If you want to all falsy values to use the fallback value, use the shorthand ternary operator (?:) instead.
Only null will return the default value. http://3v4l.org/jphnq