Php

PHP isset() vs empty() vs is_null()

in this tutorial, I’ll show you how to check if an element has been defined and see if it is empty or null. This php tutorial help to understand difference between PHP isset() vs empty() vs is_null().

These methods are used to determine a variable’s value. To see if a variable has a value, use the functions isset(), empty(), and is null().

The functions isset(), empty(), and is null() are PHP built-in functions that are used to check the value of a variable or its initialization. All these functions return a Boolean value. Here, I will explain the differences between these functions.

Function Difference Table

<div class="table-wrapper">
<table class="fl-table">
<thead>
<tr>
<th>Method Name</th>
<th>""</th>
<th>"phpflow"</th>
<th>NULL</th>
<th>FALSE</th>
<th>0</th>
<th>undefined</th>
</tr>
</thead>
<thead></thead>
<tbody>
<tr>
<td><strong><em>empty()</em></strong></td>
<td>TRUE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>TRUE</td>
<td>TRUE</td>
<td>TRUE</td>
</tr>
<tr>
<td><strong><em>is_null()</em></strong></td>
<td>FALSE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>FALSE</td>
<td>ERROR</td>
</tr>
<tr>
<td><strong><em>isset()</em></strong></td>
<td>TRUE</td>
<td>TRUE</td>
<td>FALSE</td>
<td>TRUE</td>
<td>TRUE</td>
<td>FALSE</td>
</tr>
</tbody>
</table>
</div>

PHP isset() Function

This isset() method used to determine if a variable is set and is not NULL. You can read isset() docs.

This function returns the result as a Boolean value True or False.

Syntax:

isset(variable);

Example of PHP isset() Function

$age = 0;
// Evaluates as true because $age is set
if (isset($age)) {
echo '$age is set even though it is empty';
}

Output:

$age is set even though it is empty

PHP empty() Function

The empty() is also an in-built method that is used to determine if a variable is set and not empty.

Related Post

If the value is an empty string, false, array(), NULL, 0, and an unset then empty() function returns TRUE and if a value is not empty then it returns FALSE.

Syntax:

empty( $variable )

PHP empty() example

$age = 0;
// Evaluates to true because $age is empty
if (empty($age)) {
    echo '$age is either 0, empty, or not set at all';
}

Output :

$age is either 0, empty, or not set at all

PHP is_null() Method

The is_null() method used to check whether a variable is NULL or not.

This method returns the result as a Boolean value TRUE or FALSE.

Syntax:

is_null( $variable )

Example of PHP is_null() Function

$age = null;
echo "age is " . is_null($age) . "<br>";

Output :

bool(true)
Tags: php 7

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories