Php

How To Check If Variable Is NULL Using PHP

A variable with no value is represented by null in PHP. The null value of type null is the only one that exists. PHP has a function that determines whether or not a value is null. Let’s have a look at how we can accomplish this.

We’ll also discuss PHP Null coalescing operator to assign a value to a variable if the variable doesn’t exist or null.

PHP is_null

The is_null() function in PHP determines whether a variable is NULL. The variable with no value has a unique NULL value. The only value of the type NULL is NULL.

The variable is considered to be NULL if:

  • It has been assigned a constant NULL.
  • It has not been set to any value yet.

How to check If a variable is null in PHP

The PHP is_null() function is used to test whether the variable is NULL or not. The is_null() function returns TRUE if the variable is null, FALSE otherwise. It is the case-insensitive constant NULL.

Syntax:

is_null( mixed $var ) : bool

Parameters:

$var – The variable that needs to be checked.

The Code:

<?php
  $var = null;
  if(is_null($var)) {
    echo 'The variable is NULL';
  }
  else {
    echo 'The variable is not NULL';
  }
?>

Output:

The variable is NULL

Related Post

An empty array is converted to null by non-strict equal ‘==’ comparison. Use is_null() or ‘===’ if it is possible of getting an empty array. The performance of the is_null method is much faster than ‘===’.

Method To Check NULL Using isset() and is_null()

Created a isNullOrUndefined method that takes a variable as a parameter, it’s returning True if NULL otherwise False.

function isNullOrUndefined($variable_name) {
  global $$variable_name;
  if (!isset($$variable_name) || is_null($$variable_name)) {
    return true;
  }
  return false;
}

How To Check Value:

Let’s define variables that need to test using the above method.

$foo = "foo";
$bar = null;

Output:

isNullOrUndefined("foo") //false
isNullOrUndefined("bar") //true
isNullOrUndefined("baz") //true

Check Variable Using array_key_exists Method

You can use array_key_exists('variable_name', $GLOBALS) as well, to see if a variable has been declared, but only if you know the name of the variable at coding time.

One way is if you have the variable in an array of some sort:

echo array_key_exists( $variableName, $theArray ) ? 'variable was set, possibly to null' : 'variable was never set';

If you need to check a global variable, use the $GLOBALS array:

echo array_key_exists( $variableName, $GLOBALS ) ? 'variable exists in global scope' : 'this global variable doesn\'t exist';

PHP null coalescing

PHP 7.0 added support for a null coalescing operator that is like of a ternary operator and isset(). You can get more information from null coalescing operator.

$age = $_POST['age'] ?? 32;

In the above example, the ?? is the null coalescing operator.

There are two operands it takes. The null coalescing operator returns the second operand in the event that the first operand is null or nonexistent. If not, the first one is returned.

if the variable age doesn’t exist in the $_POST array or it is null, the ?? operator will assign the string 32 to the $age variable.

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