1.2.846. No Substr Minus One¶
Negative index on string reaches offsets starting from the end of the string. This is equivalent to removing the requested offset from the full length of the string.
This syntax also prevents relying on substr() to collect characters at the end of the string.
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]; // a
echo $string[-1]; // c
//Equivalent to
echo $string[count($string) - 1]; // c
echo $string[2]; // c
?>
See also https://wiki.php.net/rfc/negative-string-offsets.
1.2.846.1. Connex PHP features¶
1.2.846.1.1. Suggestions¶
Use the -1 index in a string, instead of a call to substr()
1.2.846.1.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 |