PHP how to check variable if not empty

To check if your PHP variable is not empty, you can negate the empty() function using the bang operator (!).

The PHP empty() function is used to check whether a variable is empty or NULL.

A variable is considered empty when it has not been declared or defined with an empty string:

// 👇 this is an empty string
$name = ""; 

Here are some examples of if not empty check using PHP:

<?php
$name = "Nathan";

// 👇 if name is not empty, echo some text
if (!empty($name)) {
    echo "NAME is not empty";
}

// 👇 if age is not empty, echo some text
if (!empty($age)) {
    echo "AGE is not empty";
}

To check if a PHP variable is not empty, you can call the negated empty function or !empty() in your if statement.

Take your skills to the next level ⚡️

I'm sending out an occasional email with the latest tutorials on programming, web development, and statistics. Drop your email in the box below and I'll send new stuff straight into your inbox!

No spam. Unsubscribe anytime.