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

前端 未结 13 1722
抹茶落季
抹茶落季 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:26

    If you're looking for a one-liner to check the value of a variable you're not sure is set yet, this works:

    if ((isset($variable) ? $variable : null) == $value) { }
    

    The only possible downside is that if you're testing for true/false - null will be interpreted as equal to false.

提交回复
热议问题