Check if a variable is undefined in PHP

You can use –

$isTouch = isset($variable);

It will return true if the $variable is defined. If the variable is not defined it will return false.

Note: It returns TRUE if the variable exists and has a value other than NULL, FALSE otherwise.

If you want to check for false, 0, etc., you can then use empty()

$isTouch = empty($variable);

empty() works for –

  • “” (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • “0” (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Leave a Comment