Null Coalesce in parenthesis, in an if() Does this work as (I) expected?

24 views Asked by At

I'm just wondering if this works, using a null coalesce, in an if() statement.

With no query string, this gives me a Warning: Undefined array key:

    if($_GET['foo'] == 'bar'){ 
        doAThing();
    }

whereas

    if(($_GET['foo'] ?? 0) == 'bar'){ 
        doAThing();
    }

seems to work. Just hoping there's not some weird gotcha.

I also noticed that you can assign the value, to the previously null variable:

    if(($_GET['foo'] ??= false) == 'bar'){ 
        doAThing();
    }
    $a = $_GET['foo'];//now equals false, no warning

Which is cool, cuz now you can use $_GET['foo'] without testing for null or null coalesce.

0

There are 0 answers