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
.
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:
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
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 ‘===’.
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; }
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
array_key_exists
MethodYou 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 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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
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
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More