Empty Function in PHP: Using It Correctly And Wisely

PHP makes creating a website relatively easy. It has a large number of open sourced frameworks which can make you live within a couple hours. However, with the sophistication in language, we sometimes abandon the most fundamental concepts. This article discusses about the correct use of empty function in PHP.

Part 1: Zero(0) is treated as an empty value

This might be the most common mistake you might be doing every day. Even after making a couple of websites during my college days, I myself did not know about this until my first work experience in the Industry. Let me ask you a very simple question in the following code block:

<?php
	$testNumber = 0;
	if (empty($testNumber)) {
		echo 'Variable is empty';
	} else {
		echo 'Dude! Definitely not empty';
	}
?>

What do you think the output of this script will be? Prepare to be amazed!

/var/www/html/test$ php index.php 
Variable is empty

So, as you can see in the above block, it prints that the variable is empty! Empty function in PHP treats 0 as an empty value just like it treats as an empty string. I have seen a company lose a couple of thousand dollars just because one mighty developer who was a bit new to the organization did not know this. (No that developer was not me). Next time, be careful and don’t check for empty() for integer values as it can lead to a serious bug which you might not detect before making your feature live.

Part 2: Other Uses of Empty Function

I wanted to stress out on the behavior of empty() function with numeric zero that I created a separate part of it in my post. In this part, I will discuss the other usages of PHP.

<?php
	
	// 1.
	var_dump(empty(0));  //bool(true)

	// 2.
	var_dump(empty(1));  //bool(false)

	// 3.
	var_dump(empty('')); //bool(true)

	// 4.
	var_dump(empty('Concatly')); //bool(false)

	// 5.
	var_dump(empty(true)); //bool(false)

	// 6.
	var_dump(empty(false)); //bool(true)

	// 7.
	var_dump(empty(null)); //bool(true)

	// 8.
	var_dump(empty(array())); //bool(true)

	// 9.
	var_dump(empty(array('Not empty array'))); //bool(false)

?>

The first statement is very clearly described in part 1, so I am not going to explain it again. The second statement is also pretty clear as the integer 1 is definitely not empty.

The third statement which is an empty string returns true because the string is empty. The fourth statement contains a not empty string which is seen by a false output.

The fifth and sixth statement is again a bit tricky. Boolean true is empty while boolean false is not. PHP states that boolean true is converted to an integer 1 which is not empt while boolean false is converted to an integer 0 which is empty as seen in part 1.

In the seventh statement, a null value is empty and is shown by the printed true.

The eighth and ninth statement are also obvious as an empty array returns a boolean true while the non empty array returns false.

Conclusion

After reading this article, you must have comprehended the usage of empty function in PHP. I hope this article was helpful to you. You may read about the empty function in detail from the official PHP guide.

Please leave your valuable comments to help me improve my articles.

Spread the Knowledge
Categories:

Leave a Reply

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