The proper way to check if a variable has a value that is not null, and is not considered empty (such as 0
, 0.0
, false
, 'false'
, []
, etc.), without triggering PHP E_NOTICE messages (which I recommend displaying during development) is with the empty()
function.
// Check 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, making your code cleaner and easier to maintain.