Difference between isset() vs empty() vs is_null() in PHP
Tags: php
Published: 2022-10-18 01:43:41

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 

```php
isset(variable, ….);
```

`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
<?php
$name = null;
$result = isset($name); 

var_dump($result); // bool(false)
```

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
<?php
$name = "Pramana";
$result = isset($nama); 

var_dump($result); // bool(true)
```

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

```php
empty( $variable )
```

`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
<?php
$name = null;
$result = empty($name); 

var_dump($result); // bool(true)
```

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
<?php
$name = "";
$result = empty($name); 

var_dump($result); // bool(true)
``` 

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. 

```php
$number = 0;
var_dump(empty($number)); // bool(true)
```

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

```php
is_null( $variable )
```

`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
<?php
$name = null;
$result = is_null($name); 

var_dump($result); // bool(true)
```

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

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

var_dump($result); // bool(false)
```

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

References:
- [Difference between isset() vs empty() vs is_null() in PHP](https://kodekativ.co/a/difference-between-isset-vs-empty-vs-is-null-in-php-M3VN)
- [isset method - php.net](https://www.php.net/manual/en/function.isset.php)
- [empty method - php.net](https://www.php.net/manual/en/function.empty.php)
- [is_null method - php.net](https://www.php.net/manual/en/function.is-null.php)