php

PHP Empty() Function

Sometimes, it is required to check whether a variable is undefined or contains a False value for programming purposes. PHP has a built-in function named empty() to solve this issue. If the variable does not exist, this function does not generate any warning. The way of using the empty() function in PHP is shown in this tutorial.

Syntax:
empty(variable)

This function can only take one argument that may contain different types of data. It returns True if the variable is empty and returns False otherwise.

The empty() function returns True for the following values:

  • Zero integer (0) value
  • Zero string (“0”) value
  • Empty string (“”) value
  • FALSE value
  • NULL value
  • Empty array (array())
  • Undefined variable

Different Examples of the Empty() Function

The different uses of the empty() function are shown in this part of the tutorial using multiple examples:

Example 1: Check the Output of Empty() Function for Different Values

Create a PHP file with the following script that prints the output of the empty() function for different types of values. The return values of nine different values are checked in this script.

<?php

//Declare different types of variables
$var1 = 0;
$var2 = "0";
$var3 = 0.0;
$var4 = FALSE;
$var5 = TRUE;
$var6 = NULL;
$var7 = array();
$var8 = "";

//Check the empty() function for 0 value
echo "The empty() function returns ".empty($var1)." for the value $var1.<br/>";
//Check the empty() function for "0" value
echo "The empty() function returns ".empty($var2)." for the value $var2.<br/>";
//Check the empty() function for 0.0 value
echo "The empty() function returns ".empty($var3)." for the value $var3.<br/>";
//Check the empty() function for FALSE value
echo "The empty() function returns ".empty($var4)." for the FALSE value.<br/>";
//Check the empty() function for TRUE value
echo "The empty() function returns ".empty($var5)." for the value $var5.<br/>";
//Check the empty() function for NULL value
echo "The empty() function returns ".empty($var6)." for the NULL value.<br/>";
//Check the empty() function for empty array
echo "The empty() function returns ".empty($var7)." for the empty array.<br/>";
//Check the empty() function for empty string
echo "The empty() function returns ".empty($var8)." for the empty string.<br/>";
//Check the empty() function for the undefined variable
echo "The empty() function returns ".empty($var9)." for the undefined variable.<br/>";

?>

Output:

The following output appears after executing the previous script:

Example 2: Use the Empty() Function for Validation

Create a PHP file with the following script that uses the empty() function for validation. Three variables are initialized by taking the values from the URL parameters. The empty() function is used in this script to check if the values of these variables are empty or not.

<?php
//Read three values from the URL
$name = $_GET['name'];
$age = $_GET['age'];
$profession = $_GET['prof'];

//Check the name value
if(empty($name))
echo "Name can't be empty.";
//Check the age value
elseif(empty($age))
echo "Age can't be empty.";
//Check the professional value
elseif(empty($profession))
echo "Profession can't be empty.";
//Print the values for the non-empty values
else
echo "Name: $name<br/>Age: $age<br/>Profession: $profession";

?>

Output:

The following output appears after executing the previous script if no URL parameter is given:

The following output appears after executing the previous script if only one URL parameter is given:

The following output appears after executing the previous script if two URL parameters are given:

The following output appears after executing the previous script if all URL parameters are given:

Example 3: Use the Empty() Function for the String Value

Create a PHP file with the following script that uses the empty() function to check the different positional values of the string. The value of position 5, “0”, and “L” of the string are checked by the empty() function in this script. Here, position 5 and “0” contains the value but the position “L” does not contain any value.

<?php

//Define a string value
$strval = 'Welcome to LinuxHint';
echo "The string value is : $strval";
//Check the value of the 6th position of the string value
$return_val = empty($strval[5])? "True":"False";
echo '<br/>The return value of empty() for the $strval[5] is '.$return_val."<br/>";
//Check the value of the 1st position of the string value
$return_val = empty($strval['0'])? "True":"False";
echo 'The return value of empty() for the $strval["0"] is '.$return_val."<br/>";
//Check the value of the 'L' key of the string value
$return_val = empty($strval['L'])? "True":"False";
echo 'The return value of empty() for the $strval["L"] is '.$return_val."<br/>";

?>

Output:

The following output appears after executing the previous script:

Example 4: Use the Empty() Function for the Array Values

Create a PHP file with the following script that uses the empty() function to check if each value of an array is empty or not. An array of 8 elements is used in this script that contains different types of values. The output of the empty() function for each array value is printed later.

<?php
//Declare an array of different values
$arrval = array (
0 => 10,
1 => "78",
2 => "",
3 => True,
4 => False,
5 => NULL,
6 => "Linux",
7 => array()
);

//Iterate the array values to check whether the values are empty or not
foreach ($arrval as $k => $v) {
      //Check for an empty value
     if ($v == "")
         $arrval[$k] = "empty string";
     //Set the returned value of the empty() function
     $return_val = empty($v)? "True" : "False";
     echo "The return value of empty() function for $arrval[$k] is $return_val<br>";
}
?>

Output:

The following output appears after executing the previous script:

Conclusion

The purpose of using the empty() function in the PHP script is explained in this tutorial using simple examples. We hope that the new PHP users will be able to use this function properly after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.