Difference between isset() and empty() Functions
Last Updated :
24 Sep, 2024
The isset() and empty() functions in PHP are used for variable checking but serve different purposes. isset() checks if a variable is set and not null, while empty() checks if a variable is considered empty, like 0, false, null, or an empty string.
empty() Functions
The empty() function in PHP checks whether a variable is considered “empty.” It returns true for values like 0, “0”, false, NULL, empty arrays, empty strings, or if the variable is not set.
Syntax:
bool empty ( $var )
Parameter: This function accepts a single parameter as shown in above syntax and described below.
- $var: Variable to check whether it is empty or not.
Example: In this example we demonstrates the empty() function, which returns True for variables considered “empty” (e.g., 0, 0.0, “0”, NULL, false, empty arrays, and undefined variables).
PHP
<?php
// PHP code to demonstrate the working
// of empty() function
$var1 = 0;
$var2 = 0.0;
$var3 = "0";
$var4 = NULL;
$var5 = false;
$var6 = array();
$var7 = "";
// For value 0 as integer
empty($var1) ? print_r("True\n") : print_r("False\n");
// For value 0.0 as float
empty($var2) ? print_r("True\n") : print_r("False\n");
// For value 0 as string
empty($var3) ? print_r("True\n") : print_r("False\n");
// For value Null
empty($var4) ? print_r("True\n") : print_r("False\n");
// For value false
empty($var5) ? print_r("True\n") : print_r("False\n");
// For array
empty($var6) ? print_r("True\n") : print_r("False\n");
// For empty string
empty($var7) ? print_r("True\n") : print_r("False\n");
// For not declare $var8
empty($var8) ? print_r("True\n") : print_r("False\n");
?>
OutputTrue
True
True
True
True
True
True
True
isset() Function
The isset() function in PHP checks whether a variable is set and is not NULL. It returns true if the variable has a value other than NULL, and false if it’s unset or NULL.
Syntax:
bool isset( mixed $var [, mixed $... ] )
Parameters: This function accepts one or more parameters as mentioned above and described below.
- $var: It contains the variable which needs to check.
- $…: It contains the list of other variables.
Return Value: It returns TRUE if var exists and its value is not equal to NULL and FALSE otherwise.
Example : In this example we use isset() to check if a variable or array element is set. It confirms if $str is set and checks if the first element of $arr is defined.
PHP
<?php
$str = "GeeksforGeeks";
// Check value of variable is set or not
if(isset($str)) {
echo "Value of variable is set";
}
else {
echo "Value of variable is not set";
}
$arr = array();
// Check value of variable is set or not
if( !isset($arr[0]) ) {
echo "\nArray is Empty";
}
else {
echo "\nArray is not empty";
}
?>
OutputValue of variable is set
Array is Empty
PHP program using both isset() and empty() functions:
Example: In this example we demonstrates isset() and !empty() functions. It first checks if $num is set with isset(), then verifies if $num is not empty using the !empty() function.
PHP
<?php
// PHP function to demonstrate
// isset() and !empty() function
// initialize a variable
$num = '0';
// Check isset() function
if( isset ( $num ) ) {
print_r( $num . " is set with isset function");
}
// Display new line
echo "\n";
// Initialize a variable
$num = 1;
// Check the !empty() function
if( !empty ( $num ) ) {
print_r($num . " is set with !empty function");
}
?>
Output:
0 is set with isset function
1 is set with !empty function
Difference between isset() and empty() function:
Aspects | isset() Function | empty() Function |
---|
Purpose | Checks if a variable is declared and is not NULL . | Checks if a variable is empty or contains a falsy value. |
Undefined Variables | Generates a warning or notice if the variable does not exist. | Does not generate any warning for undefined variables. |
Return Value | Returns true if the variable is set and not NULL . | Returns true if the variable is empty (0 , null , etc.). |
Handling of Falsy Values | Returns true for 0 , "0" , false , and empty strings. | Returns true for 0 , "0" , false , null , and empty strings. |
Use Case | Used to check if a variable exists or is defined. | Used to check if a variable has no value or is empty. |
Similar Reads
What is the difference between == and === in PHP ?
In this article, we will discuss the differences between '==' and '===' operators in PHP. Both are comparison operators used to compare two or more values. == Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: oper
2 min read
Difference between the (=), (==), and (===) operators in PHP
In PHP, the '=' operator is used for assignment, while the '==' operator is used for loose equality comparison, meaning it checks if two values are equal without considering their data types. On the other hand, the '===' operator is used for strict equality comparison, meaning it checks if two value
3 min read
Difference Between JOIN, IN and EXISTS Clause in SQL
SEQUEL widely known as SQL, Structured Query Language is the most popular standard language to work on databases. We can perform tons of operations using SQL which includes creating a database, storing data in the form of tables, modify, extract and lot more. There are different versions of SQL like
4 min read
Difference between != and is not operator in Python
In this article, we are going to see != (Not equal) operators. In Python != is defined as not equal to operator. It returns True if operands on either side are not equal to each other, and returns False if they are equal. Whereas is not operator checks whether id() of two objects is same or not. If
3 min read
D3.js | d3.set.empty() Function
The set.empty() function in D3.js is used to return true if the given array has zero elements otherwise returns false. Syntax: d3.set([Array]).empty(); Parameters: This function accepts single parameter Array which is going to be searched whether it is empty or not. Return Value: It returns true if
2 min read
What is the Difference Between NOT EXISTS and NOT IN SQL Server?
When applying filters on a result set that is stored in SQL Server, there are occasions where one wants to apply conditions where specific records exist or do not exist. Not exists and not in are two of the most basic, but deciding when to use them can be a real challenge. In this article, we will l
8 min read
Difference between Set.clear() and Set.delete() Methods in JavaScript
Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes
2 min read
Explain the differences between empty() remove() and detach() methods in jQuery
If you are building something and want to remove some HTML elements on click, hover, or on any event then you should know that jQuery can do it easily for us. There are three jQuery methods that help us to remove HTML elements with some slight differences. These methods are: remove() Method: It comp
3 min read
Difference Between WHERE FIND_IN_SET(â¦)>0 and WHERE FIND_IN_SET(â¦) in SQL
FIND_IN_SET(...): This function returns the index(starting from 1) of the required string in a given list of strings if it is present in the list. If the required string is absent in the given list of strings, then 0 is returned. Syntax: SELECT FIND_IN_SET(REQUIRED_STRING, LIST_OF_STRINGS);FIND_IN_S
2 min read
Differences between undeclared and undefined variables in JavaScript
In JavaScript, variables are declared using keywords like var, let, or const. The scope and behavior of variables depend on how they are declared: var: Has function scope or global scope. If not declared explicitly, a var variable becomes an undeclared global variable when assigned a value.let and c
4 min read