The variable is considered empty if it does not exist or its value equals FALSE. The empty() does not generate a warning if the variable does not exist.
How to Check If a Variable is Empty in PHP
To check if a variable is empty in PHP, use the empty() function. The empty() is a built-in PHP function used to determine whether the variable is considered empty.
The empty() function accepts a single parameter, as shown in the above syntax and described below. It’s the opposite of the PHP isset() function we have seen earlier. The empty() function checks whether the variable is empty.
Syntax
See the following syntax.
bool empty ($var)
Parameters
The $var is the variable being checked and is the required parameter.
Below PHP 5.5, empty() only supports variables; anything else will result in a parse error. The following statement will not work empty(trim($var)). Instead, use trim($name) == false.
Return Value
It returns FALSE when $var exists and has a non-empty, non-zero value. Otherwise, it returns TRUE.
These values are considered to be an empty values:
- “(an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- “0” (0 as a string)
- NULL
- FALSE
- array() (an empty array)
See the following examples.
PHP empty() function with string
See the following code.
<?php $mage = ''; if(empty($mage)) { echo 'It\'s empty'; } else { echo 'Eleven is full'; }
See the output.
➜ pro php app.php It's empty ➜ pro
If a variable is not defined or does not exist, it will be considered empty.
If you have declared a variable and are not given any value, this will still be considered empty.
PHP empty() function with integer
See the following code.
<?php $num = 0; if(empty($num)) { echo 'It\'s empty'; } else { echo 'It is not'; }
See the output.
➜ pro php app.php It's empty ➜ pro
PHP empty() function with Array
See the following code.
<?php $num = []; if(empty($num)) { echo 'It\'s empty'; } else { echo 'It is not'; }
See the output.
➜ pro php app.php It's empty ➜ pro
An empty array is also taken as “empty”.
PHP empty() Function with Boolean
See the following code.
<?php $app = FALSE; if(empty($app)) { echo 'It\'s empty'; } else { echo 'It is not'; }
See the output.
➜ pro php app.php It's empty ➜ pro
If a Boolean variable is False, this is also considered empty.
PHP empty() Function with Object
Let’s define the empty class, create an object, and then check it with an empty() function.
<?php class App { } $data = new App(); if(empty($data)) { echo 'Class is empty'; } else { echo 'Class is not empty'; }
See the output.
➜ pro php app.php Class is not empty ➜ pro
That means that $data is not empty, and some properties are there.
A variable with NULL value is also taken as empty.
Complete Example
See the following complete example.
<?php $varA = 0; $varB = 0.0; $varC = "0"; $varD = NULL; $varE = false; $varF = array(); $varG = ""; empty($varA) ? var_dump("True\n") : var_dump("False\n"); empty($varB) ? var_dump("True\n") : var_dump("False\n"); empty($varC) ? var_dump("True\n") : var_dump("False\n"); empty($varD) ? var_dump("True\n") : var_dump("False\n"); empty($varE) ? var_dump("True\n") : var_dump("False\n"); empty($varF) ? var_dump("True\n") : var_dump("False\n"); empty($varG) ? var_dump("True\n") : var_dump("False\n");
See the following output.
➜ pro php app.php string(5) "True " string(5) "True " string(5) "True " string(5) "True " string(5) "True " string(5) "True " string(5) "True " ➜ pro
That’s it for this tutorial.