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
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
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
What is the difference between var_dump() and print_r() in PHP ?
In this article, we will discuss the difference between var_dump() and print_r() function in PHP. var_dump() Function: The var_dump() function is used to dump information about a variable that displays structured information such as the type and value of the given variable. Syntax: void var_dump ($e
3 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
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
Difference between $a != $b and $a !== $b
$a!=$b This operator is also known as inequality operator. It is used to check the equality of both operands, however, the type of the operands does not match. For instance, an integer type variable is equivalent to the floating point number if the value of both the operands is same. It is a binary
2 min read
What is the difference between null=True and blank=True in Django?
When working with Django models, you may come across two common field options: null=True and blank=True. While they may seem similar at first glance, they serve distinct purposes and have a significant impact on how data is stored and validated in your application. Difference between null=True and b
4 min read
What is the difference between (NaN != NaN) & (NaN !== NaN)?
NaN as in most programming languages means Not A Number. It belongs to the numeric data types(int, long, short, etc). It represents a numeric value that can not be interpreted as a finite value. That is one can not define its value. The use of NaN is quite rare. NaN is a property of the global objec
2 min read