Search code examples
phpif-statementnotice

Check if Variable exists and === true


I want to check if:

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

Is it possible to check this with one if statement?

Checking if === would do the trick but a PHP notice is thrown. Do I really have to check if the field is set and then if it is true?


Solution

  • If you want it in a single statement:

    if (isset($var) && ($var === true)) { ... }
    

    If you want it in a single condition:

    Well, you could ignore the notice (aka remove it from display using the error_reporting() function).

    Or you could suppress it with the evil @ character:

    if (@$var === true) { ... }
    

    This solution is NOT RECOMMENDED