Updated April 18, 2023
Introduction to PHP empty
Empty function in PHP helps us to check whether the variable or input is empty or not. By the use of this variable, we can identify our variable before using them in our program. As the name suggests empty function is responsible for checking variable contain any value inside it or not, empty function helps us to get a future exception in our program or application. The empty function works like any other programming language function which checks or validate a variable is empty or not in PHP. The empty method takes on parameter.
Syntax of PHP empty
As this is a function, so it takes parameter while using it on our program. Basically, this function takes one parameter while using it and check that parameter.
Let’s see its syntax as per the PHP doc:
boolean empty ( $your_variable )
As you can see in the above syntax, this function takes one parameter as the input. Based on the result, it will return true or false.
Let’s see one practice syntax for better understating to how to use this function in our program:
Example:
Code:
var demo = "some string";
empty($demo)
How do empty Function work in PHP?
As of now, we know that the empty function is used to check the variable is empty or not. This is an in built function in PHP that means it is already implemented in PHP. By the use of this function, we can check our string or variable very easily whether they contain any value inside them or not. This helps us prevent our code from being thrown NULL pointer exception.
First, we will see its signature in PHP how it look like:
boolean empty ($variable)
As of now, we can see this function takes one parameter as the input here. This parameter will be the variable which we want to evaluate by using an empty function in PHP. Also, the return type for this method id Boolean. Which means it will return true or false based on the result we got. It will return true if the variable that we have passed does not contain any value inside it. It will return false if the variable we have passed contains a value inside it.
Let’s see one practical example for this function to make use of it in our application; after this, we will see how it makes our code efficient and less error occurred.
Example:
Code:
<?php
$myvar = 0;
if (empty($myvar)) {
echo "This is an empty variable!!";
}
?>
As you can see in the above example, we are using the empty method to evaluate our variable in PHP. First, we have created one local variable here named as ‘myvar’ and assigned the value as ‘0’. After this, we are passing this variable inside the empty function in PHP. After calling, it will start evaluating our variable, whether it is empty or not. PHP has given some of the default values, which are taken as empty, which we will discuss later ‘0’ is one of them.
Immediately after this, we are writing one conditional statement here to check our variable against the empty function. If the variable is empty, then it will print the message; otherwise, it will not go inside the conditional statement. We can also use this method directly, not necessary to use always with the ‘IF’ statement. Here, in this case, the variable is coming out to be empty so that it will print out the message.
Now let’s see some of the default values given by the PHP for this function to be considered variable as empty, which are as follows:
a. NULL: This is the most command assignment of a variable when we have a value of a variable is NULL.
Example:
Code:
@myvar demo = NULL;
b. FALSE: This is when we assign a false value to the variable.
Example:
Code:
@myvar demo = false;
c. 0: This is when we assign a ‘0’ value to the variable.
Example:
Code:
@myvar demo = 0;
d. array (): This is when we assign array() as empty.
Example:
Code:
@myvar demo = array();
e. 0.0: This is when we assign a ‘0.0’ value to the variable.
Example:
Code:
@myvar demo = 0.0;
f. “”: This is when we assign false “” to the variable that means an empty string to a variable.
Example:
Code:
@myvar demo = "";
g. “0”: This is when we assign “0” as a zero string to the variable.
Example:
Code:
@myvar demo = "0";
Some of the advantages to use this:
- By providing the checks before, we can make our code less toward the error.
- Prevent our code from unnecessary exceptions which can occur during the null values access in the application.
Example of PHP empty
Given below is the example of PHP empty:
In this example, we are creating multiple variables and assign them some values and keep some of them as empty to check their functionality.
Code:
<!DOCTYPE html>
<html>
<body>
<p>Demo for EMPTY function in PHP</p>
<?php
$myvar1 = 0;
$myvar2 = "some value";
$myvar3 = "";
$myvar4 = NULL;
$myvar5 = 200;
$isempty1 = empty($myvar1);
$isempty2 = empty($myvar2);
$isempty3 = empty($myvar3);
$isempty4 = empty($myvar4);
$isempty5 = empty($myvar5);
echo "Result of all variables is shown below.<br>";
print $isempty1;
print $isempty2;
print $isempty3;
print $isempty4;
print $isempty5;
echo "<br>";
if ($isempty1) {
echo "TRUE.<br>";
}
if ($isempty2) {
echo "TRUE.<br>";
}
if ($isempty3) {
echo "TRUE.<br>";
}
if ($isempty4) {
echo "TRUE.<br>";
}
if ($isempty5) {
echo "TRUE.<br>";
}
echo "****************************************<br>";
?>
</body>
</html>
Output:
Conclusion
In this way, we can use this method to test or check our variable if they contain some value inside them or not. But it is an in built method, so they have defined some default values for the variable on the basis of which they return the value.
Recommended Articles
This is a guide to PHP empty. Here we discuss the introduction, syntax, and working of empty functions in PHP along with an example and output. You may also have a look at the following articles to learn more –