Check if Variable exists and === true

Sebastian Sebald picture Sebastian Sebald · Oct 26, 2011 · Viewed 67.7k times · Source

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?

Answer

Madara's Ghost picture Madara's Ghost · Oct 26, 2011

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