PHP empty vs isset

php.png

In this tutorial, we would love to share with you, PHP empty() and isset() function with its definition, syntax, require parameters and with examples. Also, we will explain the differentiate empty() and isset().

Let's start the tutorial

PHP empty() function

empty() is a inbuilt function of PHP. and commonly is to test/check if a given variable value is empty or not. If the variable value is not empty, this function will return the boolean value false. Otherwise, return the boolean value true.

Syntax:

empty( $variable )

Parameters of empty() function

The PHP empty() function will accept a one parameter as shown in above given syntax.

Example of empty() function

In the example below, we have two variables, the first is num1 and the second is num2. We will first check and test it with empty() giving the value in the variable. And also we will set the null value of the second variable, we will check with an empty function.

You can see the example below:

";

// PHP Script to test second variable
// test variable value with empty
$num2; 

if( empty( $num2 ) ) { 
	print_r(" The variable value is empty, checked by empty() "); 
}else{
	print_r(" The variable value is not empty, checked by empty() "); 
}

?>

PHP isset() function

isset() is a inbuilt function of PHP. which is used to test/check if a variable value is set or not. If the variable value is set, this function will return boolean value true. Otherwise return boolean value false.

Syntax:

isset(variable, ….);

Parametes of isset() function

The PHP isset() function will accept a multiple variable as shown in above given syntax.

Note: If multiple variables are supplied, after that this function will return true only if all of the variables are set.

Example of isset function

Let's take an example of a function isset(). In the example below, we have two variables the same as the above program, the first is num1 and the second is num2. We will first check and test it with isset() set the value in the variable. And also we will not set the value of the second variable, we will check with an isset() function.

You can see the example below:

";
} 

// PHP Script to test without set variable value
$num2; 

if( isset( $num2 ) ) { 
	print_r(" The variable value is set, checked by isset() "); 
}else{
	print_r(" The variable value is not set, checked by isset() "); 
}

?>

What is the difference between isset and empty?

isset() function

  1. TRUE if the string contains False, 0, or Empty string value.
  2. return FALSE otherwise.

empty() function

  1. TRUE if the string contains empty, 0, NULL, or False value,
  2. returns FALSE if the string contains a non-empty and non-zero value.

Author Bio

This article is written by tutsmake.com. You can also learn PHP, Mysql, Laravel, Codeigniter, Ajax, etc from tutsmake[.]com.