PHP isset Function | Determine if Variable is Set and Not Null

The isset Function in PHP is one of the most important functions in PHP. It is used to determine if a variable is set and not null. In this article, we will discuss the PHP isset Function. Also, we will discuss a few examples to demonstrate its usage.


boolean isset($variable1, $variable2 … $variableN);


The PHP isset Function expects one mandatory parameter. However, you can also pass multiple variables to the function. Pass the variables to check in the parameters.

Note: Isset works only with variables. It does not work with constants. For using it with constants, use PHP defined Function.

Return Value

The PHP isset Function returns true if all the variables are set and their value is not equal to NULL. Otherwise, it returns false. Also, with multiple variables, it returns true only if all the variables are not null.

If a variable has been unset using PHP unset Function, then it will return false.

PHP isset Function Diagram


Let’s discuss a few examples of using the isset Function.

Example 1: Basic Not Null Cases


	$integer = 123;
	/* true */ //Value of $integer is not null

	$string = 'Concatly';
	/* true */ //Value of $string is not null

In the above example, both the integer and string variable have not null values. The isset returns true for both of them.

Example 2: Empty Variables

For instance, consider empty variables as input to the isset Function.


	$integer = 0;
	/* true */ //$integer id is empty, however it is set an not null

	$string = '';
	/* true */ //$string is empty but set and not null

If you pass empty variables to the PHP isset Function. It still returns true as the values are not null.

Example 3: Isset on Null Variables

Similarly, you can pass null variables to the Function. However, in this case, the function returns false.


	/* false */ //The given variable is not defined yet. isset returns false.

	$apple = null;
	/* false */ //Value of $apple is null. isset Function returns false


Example 4: Isset on Arrays

For example, consider an an array input to the isset Function. We can check whether a key in an array exists or not just like array_key_exists Function.


	$testArray = array(
		'a' => 'apple',
		'b' => null,
		'c' => ''
	/* true */ //Key a exists and is not null

	/* false */ //Key b exists but is set to null 

	/* true */ //Key c exists and is set to empty

	/* false */ //Key d does not exist


Example 5: Isset on Objects

Similarly, you can also check for object properties using isset Function.


	class TestClass {

		public $variable1;

		public $variable2;


	$object = new TestClass();
	$object->variable1 = 1;

	/* true */ //The property variable1 is set and not null
	/* false */ //The property variable2 is not set, hence null

	/* false */ //The property variable3 does not exist, hence null


Example 6: Multiple Variables

You can pass multiple variables in the function. If all the variables are not null, only then the function returns true. It evaluates going from left to right and stops as soon as it encounters a null value.

This is similar to: isset($var1) && isset($var2) && isset($var3) …


	$variable1 = 1;
	$variable2 = 'Concatly';
	$variable3 = array();

	var_dump(isset($variable1, $variable2, $variable3));
	/* true */ //All variables are set and not null


It returns false if even one of the value is null.


	$variable1 = 1;
	$variable2 = 'Concatly';
	$variable3 = array();
	$variable4 = null;

	var_dump(isset($variable1, $variable2, $variable3, $variable4));
	/* false */ //$variable4 is null and hence isset returns false



In conclusion, we discussed the PHP isset Function in this article. You can learn more about it on Official Documentation of PHP. You learn more about PHP on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *