1.2.30. Always Positive Comparison

Some PHP native functions, such as count(), strlen(), or abs() only returns positive or null values.

When comparing them to 0, the following expressions are always true and should be avoided.

<?php

$a = [1, 2, 3];

var_dump(count($a) >= 0);
var_dump(count($a) < 0);

?>

1.2.30.1. Suggestions

  • Compare count() to non-zero values

  • Use empty()

1.2.30.2. Specs

Short name

Structures/NeverNegative

Rulesets

All, Analyze, CE, CI-checks

Exakat since

0.8.4

PHP Version

All

Severity

Major

Time To Fix

Instant (5 mins)

Precision

Very high

Examples

Magento

Available in

Entreprise Edition, Community Edition, Exakat Cloud