1.2.1161. String Int Comparison

While PHP allows direct comparison of integer and strings, with some type conversion, the rules of conversion changed in PHP 8.0. This lead to a change in behavior for comparison.

In particular, strings that are equal to 0, or empty strings, have changed.

This doesn’t affect identity comparison, since the type is initially checked.

<?php
                        PHP 7   PHP 8
var_dump(0 == "0");          true    true
var_dump(0 == "0.0");        true    true
var_dump(0 == "foo");   false   false

var_dump(0 > '');       false   true
var_dump(0 < '');       false   false
var_dump(0 >= '');      true    true
var_dump(0 <= '');      true    false

?>

See also String to Number Comparison.

1.2.1161.1. Suggestions

  • Force a conversion to integer before the comparison to make sure of the behavior.

1.2.1161.2. Specs

Short name

Php/StringIntComparison

Rulesets

All, Changed Behavior, CompatibilityPHP80

Exakat since

2.3.9

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 8.0 - More

Precision

Medium

Features

comparison

Available in

Entreprise Edition, Exakat Cloud