Check if Variable exists and === true

前端 未结 4 1059
谎友^
谎友^ 2020-12-07 00:52

I want to check if:

  • a field in the array isset
  • the field === true

Is it possible to check this with one if statement?

4条回答
  •  再見小時候
    2020-12-07 01:30

    You can simply use !empty:

    if (!empty($arr['field'])) {
       ...
    }
    

    This is precisely equivalent to your conditions by DeMorgan's law. From PHP's documentation, empty is true iff a variable is not set or equivalent to FALSE:

      isset(x) && x
      !(!isset(x) || !x)
      !empty(x)
    

    As you can see, all three of these statements are logically equivalent.

提交回复
热议问题