PHP isset() function

The isset() function is a built-in function of PHP, which is used to determine that a variable is set or not. If a variable is considered set, means the variable is declared and has a different value from the NULL. In short, it checks that the variable is declared and not null.

This function returns true if the variable is not null, otherwise it returns false. Note that the null character ("\0") is considered different from the PHP NULL constant.

Note: An important point to be noticed that if a variable is unset using the unset() function, it will not be considered to be set for so long.

Syntax

snippet
isset (mixed variable, ….)

Mixed denotes that the parameter may be multiple types.

Parameters

This function accepts one or more parameters, but it must contain atleast one parameter.

variable (required) - It is necessary to pass this parameter to this function because it holds that variable/element, which is to be checked.

… - More variables to be checked. These are optional to pass in this function.

Return Values

The isset() function returns a boolean value: It may be either true or false.

If the variable exists and does not contain a NULL value, it returns TRUE otherwise, it returns FALSE.

Changes

From PHP 5.4.0, non-numeric offsets of strings return FALSE.

Examples

Below some list of examples are given through which you can understand isset() much better-

Example 1

snippet
<?php
	$var1 = 'test';
	if(isset($var1)) {
		echo "The variable $var1 is set, so it will print. </br>";
		var_dump(isset($var1));
	}
?>

Output

Output
The variable test is set, so it will print. bool(true)

Example 2: Difference between null character and NULL constant

The null character ("\0") is different from the PHP NULL constant. With the help of below example, you can practically see the difference.

snippet
<?php
	$x = 9;
	//TRUE because $x is set
	if ($resultX = isset ($x)) {
		echo "Variable 'x' is set.";
	}
	echo var_dump(isset($resultX)). "</br>";
	
	$y = NULL;
	//False because $y is Null
	if (isset ($y)) {
		echo "Variable 'y' is not set.";
	}
	echo var_dump(isset($y)). "</br>";
	
	$z ='\0';
	//TRUE because \0 and NULL are treated different
	if ($resultZ = isset ($z)) {
		echo "Variable 'z' is set.";
	}
	echo var_dump(isset($resultZ));
?>

Output

Output
Variable 'x' is set. bool(true) bool(false) Variable 'z' is set. bool(true)

Example 3: Use of unset()

In this example, we will use the unset() function to unset the variable. Look at the below example:

snippet
<?php
	$var1 = 'test';
	$var2 = 'another test';
	if (isset($var1) && isset( $var2)) {
		echo "It will print because variables are set. </br>";
		var_dump (isset($var1));
		var_dump (isset($var2));
	}
	unset ($var1);
	unset ($var2);
	echo "</br> </br>Variables after unset: </br>";
	var_dump (isset($var1));
	var_dump (isset($var2));
?>

Output

Output
It will print because variables are set. bool(true) bool(true) Variables after unset: bool(false) bool(false)

Example 4: Difference between isset and !empty

The isset() and !empty() functions are operated same and both return same results. The only difference between them is that if the variable is not present, the !empty() function does not generate any warning.

snippet
<?php
	$valiable1 = '0';
	//check the isset() function
	if (isset ($variable1)) {
		print_r($variable1 . " is checked by isset() function");
	}
	echo "</br>";
	
	$variable2 = 1;
	//check the !empty() function
	if (!empty ($variable2)) {
		print_r($variable2 . " is checked by !empty() function");
	}
?>

Output

Output
0 is checked by isset() function 1 is checked by !empty() function

Example 5: Checking multiple variables

snippet
<?php
	$var_test1 = '';
	$var_test2 = '';
	if (isset ($var_test1, $var_test2)) {
		echo "Variables are declared and also not null.";
	} else {
		echo "Variables are not set or null";
	}
?>

Output

Output
Variables are declared and also not null.

Example 5: isset() to check session variable

snippet
<?php
	$user = 'rookienerd';
	$_SESSION['userid'] = $user;	
	if (isset($_SESSION['userid']) && !empty($_SESSION['userid'])) {
		echo " Session is available, Welcome to $_SESSION[userid] ";
	} else {
		echo " No Session, Please Login ";
	}
?>

Output

Output
Session is available, Welcome to rookienerd
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +