How to Check If the Variable is Empty in PHP

empty() function in PHP; Through this tutorial, we will learn how to check if the variable is empty in PHP using the PHP empty() function.

How to Check If the Variable is Empty in PHP

Using the php empty(), we can check if a variable is empty in PHP. The empty() is a built-in PHP function used to determine whether the variable is considered empty.

empty() function in PHP

PHP empty() function is used to check whether if a variable is empty or not. It will return True if the given variable is empty and false. If it exists or a non-zero value or any value in it, it will return false.

Syntax – empty() function

empty($variable);

In PHP empty funciton the given variables can be of different types like an integer, string or even an array etc.

Example – empty function

Let’s take an example of the PHP empty() function. In the following example, we will declare a variable and given a null value.

 

Source code – PHP empty() function



PHP empty() function is used to check whether a variable is empty or not. | PHP Tutorial



To check a whether a variable is empty or not

Second Example – emtpy() function

 

Second Example Source code – empty() function



PHP empty() function is used to check whether a variable is empty or not. | PHP Tutorial



To check a whether a variable is empty or not

Empty() example with array elements

Let’s take a new example of empty() function with array elements. Here we will declare an array and assigned a value of the array elements. And here we will assign a 0 value of the third element of the array, remaining array elements assign a non zero value.

//The empty function example
$arr = array(); 
$arr["john"] = 100;
$arr["mack"] = 50;
$arr["rose"] = 0;

foreach($arr as $key => $value){
    if (empty($value)){
    echo "(This is empty) " . $key . " = " .$value ."
"; } else{ echo "(This is not empty) " . $key . " = " .$value ."
"; } }

Source Code – Empty() example with array elements



PHP Empty() example with array elements | PHP Tutorial



PHP Empty() example with array elements

$value){ if (empty($value)){ echo "(This is empty) " . $key . " = " .$value ."
"; } else{ echo "(This is not empty) " . $key . " = " .$value ."
"; } } ?>

Why we use the empty function?

Sometimes, you need to check if a variable contains a value or not before proceeding to some action.

For example, you are taking the input values from the user in a web form. A few fields like Name, email, address are mandatory in the form. While you can check it at the client side by using the JavaScript, however, sometimes it may be disabled.

In that case, you may check the variable values by using the empty function to ensure that a user has entered some values or otherwise redirect the user back to the form page rather than proceeding to save the information in the database.

How to use PHP empty function

Using the empty function is quite simple. You simply need to provide a variable name to check if it contains a value or is it is empty.

If your string contains no characters or it is like this:

Str = “”;

Then this will be considered as empty.

  • Declared a variable is not defined or does not exist, it will be considered as empty.
  • Sometimes, you have declared a variable and not given any value, this will still be considered as empty.
  • If a Boolean variable is False, this is also considered as empty.
  • The integer or float variable with 0 value are also considered as empty.
  • A variable with NULL value is also taken as empty.
  • An empty array is also taken as “empty”.

Conclusion

empty() function in PHP; Through this tutorial, we have learned how to check if the variable is empty in PHP using the PHP empty() function.

Recommended PHP Tutorials



Images mentioned above related to PHP are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is senior ICT professional who talks about #it, #cloud, #servers, #software, and #innovation. Rabins is also the first initiator of Digital Nepal. Facebook: rabinsxp Instagram: rabinsxp

Leave a Reply

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