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.

Syntax

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

Parameters

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

Examples

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

Example 1: Basic Not Null Cases

<?php

	$integer = 123;
	var_dump(isset($integer));
	/* true */ //Value of $integer is not null

	$string = 'Concatly';
	var_dump(isset($string));
	/* 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.

<?php

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

	$string = '';
	var_dump(isset($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.

<?php

	var_dump(isset($variableDoesNotExist));
	/* false */ //The given variable is not defined yet. isset returns false.

	$apple = null;
	var_dump(isset($apple));
	/* 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.

<?php

	$testArray = array(
		'a' => 'apple',
		'b' => null,
		'c' => ''
	);
	var_dump(isset($testArray['a']));
	/* true */ //Key a exists and is not null

	var_dump(isset($testArray['b']));
	/* false */ //Key b exists but is set to null 

	var_dump(isset($testArray['c']));
	/* true */ //Key c exists and is set to empty

	var_dump(isset($testArray['d']));
	/* false */ //Key d does not exist

?>

Example 5: Isset on Objects

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

<?php

	class TestClass {

		public $variable1;

		public $variable2;

	}

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

	var_dump(isset($object->variable1));
	/* true */ //The property variable1 is set and not null
	
	var_dump(isset($object->variable2));
	/* false */ //The property variable2 is not set, hence null

	var_dump(isset($object->variable3));
	/* 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) …

<?php

	$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.

<?php

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

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

?>

Conclusion

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
Categories:

Leave a Reply

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