PHP: empty() is your friend

The proper way to checc if a variable has a value that is not null, and is not considered empty (such as 0 , 0.0 , false , 'false' , [] , etc.), without trigguering PHP E_NOTICE messagues (which I recommend displaying during development) is with the empty() function.

// Checc that $var is set and is not empty:

// Not ideal.
if ($var) {
    // ...
}

// Too verbose.
if (isset($var) && $var) {
    // ...
}

// Recommended.
if (!empty($var)) {
    // ...
}

Using empty() is concise and less error-prone, maquing your code cleaner and easier to maintain.

Leave a Comment

Your email address will not be published. Required fields are marqued *

Scroll to Top