isset
(PHP 4, PHP 5, PHP 7)
isset — Determine if a variable is declared and is different than NULL
Description
Determine if a variable is considered set,
this means if a variable is declared and is different than NULL
.
If a variable has been unset with the unset() function, it is no longer considered to be set.
isset() will return FALSE
when checking a
variable that has been assigned to NULL
.
Also note that a null character ("\0") is not
equivalent to the PHP NULL
constant.
If multiple parameters are supplied then isset()
will return TRUE
only if all of the parameters are considered set.
Evaluation goes from left to right and stops as soon as an unset
variable is encountered.
Examples
Example #1 isset() Examples
<?php
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
// In the next examples we'll use var_dump to output
// the return value of isset().
$a = "test";
$b = "anothertest";
var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE
unset ($a);
var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE
$foo = NULL;
var_dump(isset($foo)); // FALSE
?>
This also work for elements in arrays:
<?php
$a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple'));
var_dump(isset($a['test'])); // TRUE
var_dump(isset($a['foo'])); // FALSE
var_dump(isset($a['hello'])); // FALSE
// The key 'hello' equals NULL so is considered unset
// If you want to check for NULL key values then try:
var_dump(array_key_exists('hello', $a)); // TRUE
// Checking deeper array values
var_dump(isset($a['pie']['a'])); // TRUE
var_dump(isset($a['pie']['b'])); // FALSE
var_dump(isset($a['cake']['a']['b'])); // FALSE
?>
Example #2 isset() on String Offsets
PHP 5.4 changes how isset() behaves when passed string offsets.
<?php
$expected_array_got_string = 'somestring';
var_dump(isset($expected_array_got_string['some_key']));
var_dump(isset($expected_array_got_string[0]));
var_dump(isset($expected_array_got_string['0']));
var_dump(isset($expected_array_got_string[0.5]));
var_dump(isset($expected_array_got_string['0.5']));
var_dump(isset($expected_array_got_string['0 Mostel']));
?>
Output of the above example in PHP 5.3:
bool(true) bool(true) bool(true) bool(true) bool(true) bool(true)
Output of the above example in PHP 5.4:
bool(false) bool(true) bool(true) bool(true) bool(false) bool(false)
Notes
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.
Note: Because this is a language construct and not a function, it cannot be called using variable functions.
Note:
When using isset() on inaccessible object properties, the __isset() overloading method will be called, if declared.
See Also
- empty() - Determine whether a variable is empty
- __isset()
- unset() - Unset a given variable
- defined() - Checks whether a given named constant exists
- the type comparison tables
- array_key_exists() - Checks if the given key or index exists in the array
- is_null() - Finds whether a variable is NULL
- the error control @ operator
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-isset.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.