Is a PHP variable NULL or empty?
In this short PHP tutorial, we're going to check if a variable is null or empty. We'll first create a function to check for a NULL
value, then an empty value, and finally combine both functions for NULL or empty.
In this short PHP tutorial, we're going to check if a variable is null or empty. We'll first create a function to check for a NULL
value, then an empty value, and finally combine both functions for NULL or empty.
Let's start with a basic PHP function to validate whether our variable is NULL
. We'll use isset to check for NULL
.
Our isNull
function does not return true
for empty strings, we'll have to cater for that also.
echo isNull(''); //false
Next, we create our isEmpty
function.
We simply check for an empty string and return true
or false
.
Our final function is a combination of isNull
and isEmpty
The function isNullOrEmpty
will determine whether a variable is not null or empty.