PHP: Check if variable exist but also if has a value equal to something

前端 未结 13 1739
抹茶落季
抹茶落季 2020-12-01 13:46

I have (or not) a variable $_GET[\'myvar\'] coming from my query string and I want to check if this variable exists and also if the value corresponds to somethi

13条回答
  •  借酒劲吻你
    2020-12-01 14:16

    As of PHP7 you can use the Null Coalescing Operator ?? to avoid the double reference:

    $_GET['myvar'] = 'hello';
    if (($_GET['myvar'] ?? '') == 'hello') {
        echo "hello!";
    }
    

    Output:

    hello!
    

    In general, the expression

    $a ?? $b
    

    is equivalent to

    isset($a) ? $a : $b
    

提交回复
热议问题