Programming > PHP & MySQL > Which PHP variable checking function should I use to check if a variable is empty, 0, null, empty string or even exists?

  • if($var) — checks if a variable has any value that if evaluated returns true, for example an integer other than 0 or a string that is not empty will return true, (empty string, empty array, null, 0 or false will return false.
  • isset($var) — checks if a variable has been declared and is different than null — returns true if the variable exists and has any value other than null, otherwise, it returns false.
  • empty($var) — checks if a variable is empty — returns true if a variable does not exist or has a value that is empty or zero.
  • is_null($var) — checks if a variable is null - returns true if value is null, false otherwise

 Empty String ('' or "")Empty Array ([])NULL0 or "0"false
if()falsefalsefalsefalsefalse
issettruetruefalsetruetrue
emptytruetruetruetruetrue
is_nullfalsefalsetruefalsefalse