Home PHP PHP isset()

PHP isset(): A Beginner's Guide to isset() Function

By a TechBitBytes Contributor, September 07, 2023
8 MIN READ |

This PHP Arrays tutorial is designed for PHP beginners who want to learn the basics of PHP isset() function.

The isset() function checks if a variable exists (is declared) and is not set to null. If a variable exists and is set to something else other than null, isset() returns true, else, it returns false.

bool isset(var,[varN])

Using isset()

<?php
	//PHP Code Block 1
	
	$a = 32;
	
	var_dump(isset($a)); // Returns TRUE;
	var_dump(isset($b)); // Returns FALSE;
	
	$b = "";
	
	var_dump(isset($b)); // Returns TRUE;
	
	unset($a);
	var_dump(isset($a)); // Returns FALSE;
	
	$c = 14;
	var_dump(isset($a, $b)); // Returns FALSE;
	var_dump(isset($b, $c)); // Returns TRUE;

?>

Line 6 returns TRUE for isset($a) as $a is already declared in Line 4. However, the first isset($b) in Line 7 returns FALSE since $b is yet to be declared. Once it is declared in Line 9, the second isset($b) in Line 11 returns TRUE.

As demonstrated above, isset() returns TRUE if a variable is declared. It does not check if the variable is empty (or considered empty). For that, you will use PHP's empty() function.

Please note, if a variable has been unset using the unset() function, the variable is considered not to exist hence will return false. That is why, in PHP Code Block 1 above, Line 14 returns $a as false as it has been unset in Line 13.

isset() can be used to check more than one variables. If all the variables are declared and none is set to NULL, isset() returns TRUE. If one variable is not considered declared (not declared or set to NULL), isset() returns FALSE for all. In PHP Code Block 1 above, isset($a, $b) returns FALSE as $a is not declared.

isset() will not work with defined constants

You can set constants in PHP using the define() function. To check if a constant has been defined, we use the defined() function and not isset(). As earlier stated, isset() checks variables only.

isset() will not work with variable functions

PHP supports variable functions. Once a variable is assigned parenthesis, PHP will look for a function with the name that the variable evaluates to, and attempt to execute it.

<?php
	//PHP Code Block 2
	
	function showAge($age){
		echo $age." Years Old";
	}
	
	$display_age = 'showAge';
	
	$display_age('32'); // Displays "32 Years Old"
?>

The isset() function will not work when it comes to variable functions as represented below:

<?php
	//PHP Code Block 3
	
	function showAge($age){
		echo $age." Years Old";
	}
	
	$display_age = 'showAge';
	
	//$display_age("32");
	
	var_dump(isset($display_age("32")));
	// Returns an Error: Fatal error: Cannot use isset()
	// on the result of an expression
?>

You can learn more about the isset() function on PHP.net isset()

 

  This article is written to the best of the author's knowledge. TechBitBytes(TBB) ensures that all articles are constantly updated with the latest information.