How to Check If the Variable is Empty in PHP

How to Check If the Variable is Empty in PHP

An empty variable refers to a variable that has been declared but has not been assigned a value or has a value that is considered empty. An empty variable can cause unexpected behavior in your PHP script, which can lead to runtime errors or other issues.

empty() function in PHP; Through this tutorial, you will learn different techniques to check if a variable is empty in PHP.

How to Check If the Variable is Empty in PHP

In PHP, there are several ways to check if a variable is empty. You can use the below given different techniques to check if a variable is empty in PHP:

  • Using empty() Function
  • Using isset() Function
  • Using strlen() Function
  • Using is_null()

Using empty() Function

The simplest and most commonly used method to check if a variable is empty in PHP is to use the empty() function. The empty() function is a built-in PHP function that checks if a variable is considered empty. A variable is considered empty if it has no value, or if the value is NULL, 0, “”, false, array() or “0”.

Here’s an syntax of 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.

Here’s an example of using the empty() function to check if a variable is empty:

$var = '';

if (empty($var)) {
    echo 'Variable is empty';
} else {
    echo 'Variable is not empty';
}

This code initializes a variable $var to an empty string (''), then checks if the variable is empty using the empty() function.

If the variable is empty, meaning it has no value or its value is equal to false (i.e., an empty string, null, false, 0, or an empty array), the code will output 'Variable is empty' using the echo statement.

If the variable is not empty, meaning it has a value other than those mentioned above, the code will output 'Variable is not empty'.

In this case, since $var is initialized as an empty string, the code will output 'Variable is empty'.

Using isset() Function

Another common method to check if a variable is empty is to use the isset() function. The isset() function is a built-in PHP function that checks if a variable is declared and is not NULL.

Here’s an example of using the isset() function to check if a variable is empty:

$var = '';

if (isset($var) && $var !== '') {
    echo 'Variable is not empty';
} else {
    echo 'Variable is empty';
}

The above given code is a PHP code snippet that checks whether the variable $var is empty or not.

Here’s a step-by-step explanation of the code:

  1. $var = '';: This line initializes the variable $var with an empty string.
  2. isset($var): The isset() function checks whether the variable $var is set or not. In this case, since we just initialized $var with an empty string, it is considered set.
  3. $var !== '': This condition checks whether $var is not equal to an empty string. The !== operator is used to compare the value of $var with an empty string and ensure that they are not the same.
  4. echo 'Variable is empty';: If the condition in step 3 evaluates to false, i.e., $var is an empty string, this line is executed, and the output is “Variable is empty”.
  5. echo 'Variable is not empty';: If the condition in step 3 evaluates to true, i.e., $var is not an empty string, this line is executed, and the output is “Variable is not empty”.

In summary, the code checks if $var is an empty string, and if it is not, it prints “Variable is not empty”. If $var is empty, it prints “Variable is empty”.

Using strlen() Function

The strlen() function is another method to check if a variable is empty in PHP. The strlen() function is a built-in PHP function that returns the length of a string.

Here’s an example of using the strlen() function to check if a variable is empty:

$var = '';

if (strlen($var) == 0) {
    echo 'Variable is empty';
} else {
    echo 'Variable is not empty';
}

In this example, you have declared a variable $var with an empty string. You then use the strlen() function to check if the length of the variable is 0. Since the variable has an empty string value, the strlen() function will return 0 and the code inside the if statement will be executed, which will output “Variable is empty”.

Using is_null()

The is_null() function is a built-in PHP function that checks if a variable is NULL. It returns true if the variable is NULL, and false otherwise.

Here’s an example of how to use the is_null() function:

$myVar = NULL;

if (is_null($myVar)) {
    echo "The variable is empty";
} else {
    echo "The variable is not empty";
}

The code initializes a variable named $myVar with a value of NULL which means the variable has no value assigned to it.

The is_null() function is then used to check if the value of $myVar is NULL. If the value is NULL, then the code will execute the block of code within the if statement which will output the string “The variable is empty”.

If the value of $myVar is not NULL, then the code will execute the block of code within the else statement which will output the string “The variable is not empty”.

So, in this case, since the value of $myVar is explicitly set to NULL, the code will output “The variable is empty”.

Conclusion

In PHP, checking if a variable is empty is a common task that is required in many programming scenarios. There are different methods to check if a variable is empty, including using the empty() function, isset(), is_null() function, and strlen() function. Each method has its own advantages and disadvantages, and the choice of method depends on the specific programming scenario. By using these techniques, you can avoid unexpected behavior in your PHP script and improve the overall reliability and stability of your code.

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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