1.2.1417. Variable Is Not A Condition

Avoid using a lone variable as a condition. It is recommended to use a comparative value, or one of the filtering function, such as isset(), empty().

Using the raw variable as a condition blurs the difference between an undefined variable and an empty value. By using an explicit comparison or validation function, it is easier to understand what the variable stands for. Thanks to the PMB team for the inspiration.

<?php

if (isset($error)) {
    echo 'Found one error : '.$error!;
}

//
if ($errors) {
    print count($errors).' errors found : '.join('', $errors).PHP_EOL;
    echo 'Not found';
}

?>

1.2.1417.1. Suggestions

  • Make the validation explicit, by using a comparison operator, or one of the validation function.

1.2.1417.2. Specs

Short name

Structures/NoVariableIsACondition

Rulesets

All, Analyze

Exakat since

1.6.5

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Very high

Features

comparison, iffectation

Available in

Entreprise Edition, Exakat Cloud