isNull

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

isEmpty

Next, we create our isEmpty function.

We simply check for an empty string and return true or false.

isNullOrEmpty

Our final function is a combination of isNull and isEmpty

The function isNullOrEmpty will determine whether a variable is not null or empty.

Key Takeaways

  • Our code validates a string variable for null or empty.
  • We use a simple if not NULL or empty check to determine whether to return TRUE or FALSE.
  • You could update the function to trim the variable before checking for empty.
  • Related PHP functions: empty, is_null