1.2.851. No isset() With empty()

empty() actually does the job of isset() too.

From the manual : No warning is generated if the variable does not exist. That means empty() is essentially the concise equivalent to !`isset( <https://www.www.php.net/isset>`_$var) || $var == false. The main difference is that isset() only works with variables, while empty() works with other structures, such as constants.

<?php


// Enough validation
if (!empty($a)) {
    doSomething();
}

// Too many tests
if (isset($a) && !empty($a)) {
    doSomething();
}

?>

See also Isset and empty.

1.2.851.1. Connex PHP features

1.2.851.1.1. Suggestions

  • Only use isset(), just drop the empty()

  • Only use empty(), just drop the empty()

  • Use a null value, so the variable is always set

1.2.851.1.2. Specs

Short name

Structures/NoIssetWithEmpty

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior, PHP recommendations

Exakat since

0.8.7

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Examples

XOOPS

Available in

Entreprise Edition, Community Edition, Exakat Cloud