PHP – empty() Explained

The previous article explained how the isset() language construct is used to check that a variable is defined and has a value before using it. The empty() language construct is another option that can be used to check if a variable has a value that can be used.

empty() is used to check if a variable is considered empty to PHP. In PHP, empty means that the variable is not declared or it has a value that is loosely equal (==) to false. For example, the following are loosely equal to false: 0, 0.0, “”, “0”, [], NULL

Syntax: empty($var);

Output: empty() will return FALSE when checking a variable that exists and has a value that is NOT loosely equal (==) to false. Otherwise TRUE will be returned.

Unlike isset(), empty can only accept one parameter. Also unlike isset(), the parameter can be an expression while isset() only accepts variables.

Results of empty()

The PHP interactive shell can be used to quickly see if the results of empty() are true or false. The PHP interactive shell can be accessed by typing php -a at the command line.

Result Of Checking Undeclared Variable With empty()

The below screenshot shows var_dump() being used to display the data type and value of the result of empty($undeclared). In this example, the $undeclared variable has not been declared to the PHP interpreter before attempting to be used.

PHP empty var_dump of undeclared variable

Result Of empty($undeclared): true

The $undeclared variable has not been declared to the PHP interpreter so it is considered empty so true is returned.

Result Of Checking NULL Variable With empty()

The below screenshot shows a variable named $var being declared and then assigned the value null. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($var). The screenshot also shows the result of loose comparison of null == false.

PHP empty var_dump of null variable

Result Of empty($var): true

empty() checks if the variable is declared. The $var variable is declared so empty() then checks if the value of $var is loosely equal to false. As the screenshot shows, null is loosely equal to false, so empty() returns true.

Result Of Checking String Variables With empty()

The below screenshot shows a variable named $str being declared and then assigned the value of an empty string “”. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($str). The screenshot also shows the result of loose comparison of “” == false.

PHP empty var_dump empty string variable

Result Of empty($var): true

empty() checks if the variable is declared. The $str variable is declared so empty() then checks if the value of $str is loosely equal to false. As the screenshot shows, an empty string “” is loosely equal to false, so empty() returns true.

The below screenshot shows a variable named $str being declared and then assigned the value of “Hello World”. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($str). The screenshot also shows the result of loose comparison of “Hello World” == false.

PHP empty var_dump string variable

Result Of empty($var): false

empty() checks if the variable is declared. The $str variable is declared so empty() then checks if the value of $str is loosely equal to false. As the screenshot shows, a string “Hello World” is not loosely equal to false, so empty() returns true.

The below screenshot shows a variable named $str being declared and then assigned the value of “0”. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($str). The screenshot also shows the result of loose comparison of “0” == false.

PHP empty var_dump zero string variable

Result Of empty($var): true

empty() checks if the variable is declared. The $str variable is declared so empty() then checks if the value of $str is loosely equal to false. As the screenshot shows, a string “0” is loosely equal to false, so empty() returns true.

Result Of Checking Number Variables With empty()

The below screenshot shows a variable named $int being declared and then assigned the value of 0. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($int). The screenshot also shows the result of loose comparison of 0 == false.

PHP empty var_dump zero variable

Result Of empty($int): true

empty() checks if the variable is declared. The $int variable is declared so empty() then checks if the value of $int is loosely equal to false. As the screenshot shows, the number 0 is loosely equal to false, so empty() returns true.

The below screenshot shows a variable named $int being declared and then assigned the value of 1. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($int). The screenshot also shows the result of loose comparison of 1 == false.

PHP empty var_dump number variable

Result Of empty($int): false

empty() checks if the variable is declared. The $int variable is declared so empty() then checks if the value of $int is loosely equal to false. As the screenshot shows, the number 1 is not loosely equal to false, so empty() returns true.

Result Of Checking Array Variables With empty()

The below screenshot shows a variable named $arr being declared and then assigned an empty array []. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($arr). The screenshot also shows the result of loose comparison of [] == false.

PHP empty var_dump empty array variable

Result Of empty($arr): true

empty() checks if the variable is declared. The $arr variable is declared so empty() then checks if the value of $arr is loosely equal to false. As the screenshot shows, an empty array is loosely equal to false, so empty() returns true.

The below screenshot shows a variable named $arr being declared and then assigned an array of [0, 1, 2]. The screenshot then shows var_dump() being used to display the data type and value of the result of empty($arr). The screenshot also shows the result of loose comparison of [0, 1, 2] == false.

PHP empty var_dump array variable

Result Of empty($arr): false

empty() checks if the variable is declared. The $arr variable is declared so empty() then checks if the value of $arr is loosely equal to false. As the screenshot shows, an array of values is not loosely equal to false, so empty() returns false.

Conclusion

The empty() language construct is used to determine if a variable is declared and if the variable’s value is loosely equal to false. It is important to check if a variable is set when data is coming from outside sources such as an API, data POSTed from a form, or query parameters from a URL. The above examples show the results of using empty() to determine if variables are considered empty by PHP.


References:
https://www.php.net/manual/en/function.empty.php
https://www.php.net/manual/en/function.isset.php
https://kunststube.net/isset
https://code.tutsplus.com/tutorials/php-isset-vs-empty-vs-is_null–cms-37162

Leave a Comment

Your email address will not be published. Required fields are marked *