DEV Community

Ida Bagus Gede Pramana Adi Putra
Ida Bagus Gede Pramana Adi Putra

Posted on • Originally published at kodekativ.co

Difference between isset() vs empty() vs is_null() in PHP

There are some built-in PHP functions that are similar to each other, and those are isset(), empty() and is_null().

isset() method

If the variable has been set with a value, this method will return true. Otherwise, it will return false.

isset() syntax

isset(variable, .);
Enter fullscreen mode Exit fullscreen mode

isset() can take many variables as its argument, see variable, ...) on the syntax. If we put many variables into the parameter, then this method will only return true if all the variable has already been set with value.

isset() examples

Let's implement isset() method to accept two variables as its argument. On the first variable, we will assign a null as its value, and on the second variable, we will assign a string or any other data type other than null.

<?php
$name = null;
$result = isset($name); 

var_dump($result); // bool(false)
Enter fullscreen mode Exit fullscreen mode

The result from the snippet above is bool(false) as the variable that we assigned with a value was null.

However, if we assign the value with other data type than null, such example:

<?php
$name = "Pramana";
$result = isset($nama); 

var_dump($result); // bool(true)
Enter fullscreen mode Exit fullscreen mode

The code snippet above will return bool(true) because we assigned a string to the variable. Any value with a data type other than null will be interpreted as a non-null variable, therefore it returns true.

empty() method

empty() is a built-in function in PHP that's intended to check whether a variable is empty or has not been set with any value. This method returns either true or false.

empty() syntax

empty( $variable )
Enter fullscreen mode Exit fullscreen mode

empty() only accept one parameter as its argument, as the syntax shows.

empty() example

Let's implement empty() with two variables, on the first variable, we will set it with a null value, and a string value for the second variable.

<?php
$name = null;
$result = empty($name); 

var_dump($result); // bool(true)
Enter fullscreen mode Exit fullscreen mode

The code snippet above will return true as the result. It's because the empty() checks whether a variable is empty, and a null value is considered as empty, thus it returns true.

The answer is nope as it depends on the string length.

<?php
$name = "";
$result = empty($name); 

var_dump($result); // bool(true)
Enter fullscreen mode Exit fullscreen mode

The code above returns true even though it's assigned with a string and not null. This is because we assigned "" which is an empty string. The "" has no value, it's only a string but empty, and that's why it returns true.

It's a little bit tricky.

What if we assigned the variable with an integer, but the integer is 0.

$number = 0;
var_dump(empty($number)); // bool(true)
Enter fullscreen mode Exit fullscreen mode

This is also returning true, because 0 is considered as empty when we validate it against empty() method. If it was assigned with another integer other than 0, then the result would be false.

is_null() method

is_null() is a PHP built-in function to check whether a variable is assigned a null value. This method returns a boolean true or false.

is_null method

is_null( $variable )
Enter fullscreen mode Exit fullscreen mode

is_null method only accepts one parameter as its argument.

Let's implement is_null method against two variables, the first variable will hold a null value, and a non-value on the second variable.

<?php
$name = null;
$result = is_null($name); 

var_dump($result); // bool(true)
Enter fullscreen mode Exit fullscreen mode

The code above returns true because $name variable holds a null value.

<?php
$name = "Pramana";
$result = is_null($name); 

var_dump($result); // bool(false)
Enter fullscreen mode Exit fullscreen mode

However, if we use the is_null() method against a non-null value, we will get false as the result.

References:

Top comments (1)

Collapse
 
kansoldev profile image
Yahaya Oyinkansola

I never noticed isset() method could check multiple variables at once, serious game changer!, nice article. I have understood better the difference between these 3, cause it can get quite confusing at times