PHP Empty() Function | How To Check if Array is Empty PHP
Summary: In this article, we demonstrate and explore PHP empty() function and how to check if array is empty PHP with example. Let’s understand in detail this empty function with an example.
PHP Empty() Function Definition and Usage
PHP empty() function determines whether a variable is empty, this function is an inbuilt function in PHP or you can say that the empty() function checks whether a variable is empty or not. It returns false if the variable exists and is not empty, otherwise it returns true. means that a variable is considered empty if it does not exist or if its value equals FALSE.
The following values evaluate to empty:-
- 0
- 0.0
"0"
""
- array()
- FALSE
- NULL
Syntax:
To Identify whether a variable is considered to be empty. A variable is considered empty if it doesn’t exist or if its value equals false. This function doesn’t generate a warning if the variable doesn’t exist.
Parameter Value:
This function accepts a single parameter as shown in the above syntax and described below.
Parameter Value | Description |
---|---|
$var: | Required. Variable to check whether it is empty or not. |
Prior to PHP 5.5, empty() only supports the variables; anything else will result in a parse error. In other words, we can say that the following will not work: empty(trim($name)). Instead, use trim($name) == false.
No warning is generated if the variable doesn’t exist. which means empty() is essentially the concise identical to !isset($var) || $var == false.
Technical Details
PHP Version: | 4.0+ |
Return Type: | Boolean |
Return Value: | FALSE if the variable exists and isn’t empty, TRUE otherwise |
PHP Changelog: | PHP 5.5: it supports expressions, not only variables, and in PHP 5.4: Non-numeric offsets of strings, it returns TRUE |
PHP empty() Function Example
Program:1
1 2 3 4 5 6 7 8 9 10 11 |
<?php $x = 0; // True because $x is empty if (empty($x)) { echo "The variable 'x' is empty.<br>"; } // True because $x is set if (isset($x)) { echo "The variable 'x' is set"; } ?> |
'x'
is empty.Variable
'x'
is set
Program: 2
PHP 5.4 changes how empty() behaves when passed string offsets.
1 2 3 4 5 6 7 8 9 |
<?php $expected_array_got_string = 'somestring'; var_dump(empty($expected_array_got_string['some_key'])); var_dump(empty($expected_array_got_string[0])); var_dump(empty($expected_array_got_string['0'])); var_dump(empty($expected_array_got_string[0.5])); var_dump(empty($expected_array_got_string['0.5'])); var_dump(empty($expected_array_got_string['0 Mostel'])); ?> |
false
false
false
true
true
Program:3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?php $myvar1 = ''; $myvar2 = 0; $myvar3 = NULL; $myvar4 = FALSE; $myvar5 = array(); // Testing the variables if(empty($myvar1)){ echo 'This line is printed, because the $myvar1 is empty.'; } echo "<br>"; if(empty($myvar2)){ echo 'This line is printed, because the $myvar2 is empty.'; } echo "<br>"; if(empty($myvar3)){ echo 'This line is printed, because the $myvar3 is empty.'; } echo "<br>"; if(empty($myvar4)){ echo 'This line is printed, because the $myvar4 is empty.'; } echo "<br>"; if(empty($myvar5)){ echo 'This line is printed, because the $myvar5 is empty.'; } ?> |
This line is printed, because the $myvar2 is empty.
This line is printed, because the $myvar3 is empty.
This line is printed, because the $myvar4 is empty.
This line is printed, because the $myvar5 is empty.
Conclusion:
In this article, you have learned PHP empty() function with the different example. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem. Stay healthy, stay safe! Happy coding! enjoy it!