1.2.838. No Substr Minus One

Negative index were introduced in PHP 7.1. This syntax is not compatible with PHP 7.0 and older.

<?php
$string = 'abc';

echo $string[-1]; // c

echo $string[1]; // a

?>

See also Generalize support of negative string offsets.

1.2.838.1. Suggestions

  • Use the -1 index in a string, instead of a call to substr()

1.2.838.2. Specs

Short name

Php/NoSubstrMinusOne

Rulesets

All, Changed Behavior, CompatibilityPHP53, CompatibilityPHP54, CompatibilityPHP55, CompatibilityPHP56, CompatibilityPHP70

Exakat since

0.12.5

PHP Version

With PHP 7.1 and more recent

Severity

Major

Time To Fix

Quick (30 mins)

Precision

Very high

Available in

Entreprise Edition, Exakat Cloud