[edit] Last updated: Fri, 15 Mar 2013
view this page in
isset
(PHP 4, PHP 5)
isset — Determine if a variable is set and is not NULL
Description
$var
[, mixed $...
] )
Determine if a variable is set and is not NULL
.
If a variable has been unset with unset(), it will no
longer be set. isset() will return FALSE
if testing a
variable that has been set to NULL
. Also note that a NULL
byte
("\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 set. Evaluation goes from
left to right and stops as soon as an unset variable is encountered.
Parameters
-
var
-
The variable to be checked.
-
...
-
Another variable ...
Return Values
Returns TRUE
if var
exists and has value other
than NULL
, FALSE
otherwise.
Changelog
Version | Description |
---|---|
5.4.0 |
Checking non-numeric offsets of strings now returns |
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
[edit] Last updated: Fri, 15 Mar 2013
To organize some of the frequently used functions..
<?php
/**
* Returns field of variable (arr[key] or obj->prop), otherwise the third parameter
* @param array/object $arr_or_obj
* @param string $key_or_prop
* @param mixed $else
*/
function nz($arr_or_obj, $key_or_prop, $else){
$result = $else;
if(isset($arr_or_obj)){
if(is_array($arr_or_obj){
if(isset($arr_or_obj[$key_or_prop]))
$result = $arr_or_obj[$key_or_prop];
}elseif(is_object($arr_or_object))
if(isset($arr_or_obj->$key_or_prop))
$result = $arr_or_obj->$key_or_prop;
}
}
return $result;
}
/**
* Returns integer value using nz()
*/
function nz_int($arr_or_obj, $key_or_prop, $else){
return intval(nz($arr_or_obj, $key_or_prop, $else));
}
$my_id = nz_int($_REQUEST, 'id', 0);
if($my_id > 0){
//why?
}
?>
Simple solution for: "Fatal error: Can't use function return value in write context in ..."
<?php
function _isset($val) { return isset($val); }
?>
In PHP4, the following works as expected:
<?php
if (isset($obj->thing['key'])) {
unset($obj->thing['key']) ;
}
?>
In PHP5 however you will get a fatal error for the unset().
The work around is:
<?php
if (is_array($obj->thing) && isset($obj->thing['key'])) {
unset($obj->thing['key']) ;
}
?>
I tried the example posted previously by Slawek:
$foo = 'a little string';
echo isset($foo)?'yes ':'no ', isset($foo['aaaa'])?'yes ':'no ';
He got yes yes, but he didn't say what version of PHP he was using.
I tried this on PHP 5.0.5 and got: yes no
But on PHP 4.3.5 I got: yes yes
Apparently, PHP4 converts the the string 'aaaa' to zero and then returns the string character at that position within the string $foo, when $foo is not an array. That means you can't assume you are dealing with an array, even if you used an expression such as isset($foo['aaaa']['bbb']['cc']['d']), because it will return true also if any part is a string.
PHP5 does not do this. If $foo is a string, the index must actually be numeric (e.g. $foo[0]) for it to return the indexed character.
"empty() is the opposite of (boolean) var, except that no warning is generated when the variable is not set."
So essentially
<?php
if (isset($var) && $var)
?>
is the same as
<?php
if (!empty($var))
?>
doesn't it? :)
!empty() mimics the chk() function posted before.
if you want to check whether the user has sent post vars from a form, it is a pain to write something like the following, since isset() does not check for zero-length strings:
if(isset($form_name) && $form_name != '') [...]
a shorter way would be this one:
if($form_name && $form_message) [...]
but this is dirty since you cannot make sure these variables exist and php will echo a warning if you refer to a non-existing variable like this. plus, a string containing "0" will evaluate to FALSE if casted to a boolean.
this function will check one or more form values if they are set and do not contain an empty string. it returns false on the first empty or non-existing post var.
<?
function postvars() {
foreach(func_get_args() as $var) {
if(!isset($_POST[$var]) || $_POST[$var] === '') return false;
}
return true;
}
?>
example: if(postvars('form_name','form_message')) [...]
Here's a nice little function that I use everywhere that'll help with setting alternate values so you don't have a bunch of situations like:
<?php
if(isset($a))
{
$b = $a;
}
else
{
$b = "default";
}
function isset_or(&$check, $alternate = NULL)
{
return (isset($check)) ? $check : $alternate;
}
// Example usage:
$first_name = isset_or($_POST['first_name'], "Empty");
$total = isset_or($row['total'], 0);
?>
in PHP5, if you have
<?PHP
class Foo
{
protected $data = array('bar' => null);
function __get($p)
{
if( isset($this->data[$p]) ) return $this->data[$p];
}
}
?>
and
<?PHP
$foo = new Foo;
echo isset($foo->bar);
?>
will always echo 'false'. because the isset() accepts VARIABLES as it parameters, but in this case, $foo->bar is NOT a VARIABLE. it is a VALUE returned from the __get() method of the class Foo. thus the isset($foo->bar) expreesion will always equal 'false'.
If you have for example a variable in your URL say url.php?var= and some one types in %00 the variable will pass isset. For post and get variables I wrote this function to filter out varables that are set but empty.
function isval($inp){
if(isset($inp)){
$len = strlen($inp);
if($len > 0){
return true;
}
else{
return false;
}
}
else{
return false;
}
}
Note: isset() only checks variables as anything else will result in a parse error. In other words, the following will not work: isset(trim($name)).
isset() is the opposite of is_null($var) , except that no warning is generated when the variable is not set.
Below a user by the name of Scott posted an isval() function; I just wanted to point out a revision to his method since it's a bit lengthy for what it does. The trick is to realize that a boolean AND clause will terminate with fa