Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. isset() / empty() / is_null()

isset() / empty() / is_null() Since: PHP 4(2000)

Functions for checking whether a variable is defined or its value is not empty. Commonly used for form input validation and checking whether an array key exists.

Syntax

// Checks that a variable is set and is not null.
isset($var, $var2, ...);

// Checks whether a variable is empty.
empty($var);

// Checks whether a variable is null.
is_null($var);

// Destroys a variable.
unset($var, $var2, ...);

Function List

FunctionDescription
isset($var, ...)Returns true if a variable is set and its value is not null. You can specify multiple variables; true is returned only if all of them are set.
empty($var)Determines whether a variable is empty. Undefined variables, null, false, 0, "", "0", and empty arrays are all considered empty.
is_null($var)Determines whether a variable is null. Using it on an undefined variable triggers a warning.
unset($var, ...)Destroys the specified variable(s). Multiple variables can be specified at once.

Values That Make empty() Return true

Valueempty() result
nulltrue
falsetrue
0true
0.0true
""true
"0"true
[]true
Undefined variabletrue

Sample Code

<?php
// Use isset() to check whether a variable exists.
$name = "John Doe";
var_dump(isset($name)); // Outputs bool(true).
var_dump(isset($undefined)); // Outputs bool(false) because the variable is not defined.

// A null variable returns false from isset().
$value = null;
var_dump(isset($value)); // Outputs bool(false).

// You can check multiple variables at once.
$a = 1;
$b = 2;
var_dump(isset($a, $b)); // Outputs bool(true) because all variables are set.

// Use empty() to check whether a variable is empty.
var_dump(empty("")); // Outputs bool(true).
var_dump(empty("0")); // The string "0" is also considered empty.
var_dump(empty("abc")); // Outputs bool(false).

// A common pattern for validating form input.
$_POST['email'] = "user@example.com";
if (!empty($_POST['email'])) {
	echo "An email address has been entered."; // This line is output.
}

// Also useful for checking whether an array key exists.
$config = ['debug' => false, 'version' => '1.0'];
var_dump(isset($config['debug'])); // Outputs bool(true).
var_dump(isset($config['author'])); // Outputs bool(false) because the key does not exist.

// Use unset() to destroy a variable.
$temp = "temporary data";
unset($temp);
var_dump(isset($temp)); // Outputs bool(false) because the variable has been destroyed.

// is_null() checks for null only.
$val = null;
var_dump(is_null($val)); // Outputs bool(true).
var_dump(is_null("")); // Outputs bool(false) because an empty string is not null.

Notes

isset() checks that a variable is set and not null, while empty() checks whether a variable is empty. Because empty() treats "0" and 0 as empty, using it to validate numeric input can produce unexpected results. To check whether a value is a valid number, use is_numeric().

isset() and empty() are language constructs, so using them on undefined variables does not trigger a warning. In contrast, is_null() is a regular function, and using it on an undefined variable will produce a Warning.

A common pattern for validating form input is to use isset($_POST['key']) to confirm the key exists, and !empty($_POST['key']) to confirm a value was entered. For more on reading request data, see $_GET / $_POST.

If you find any errors or copyright issues, please .