Search code examples
phpstringversiondocumentation

Which term do I have to look for in the php-documentation for information about string offsets by using $string[$number]?


I get the notice:

php Notice: Uninitialized string offset: -1

for the codeline

if ($a[-1]==='/') { $a= substr($a,0,-1); }

Where could I find the description of the $string[$number] syntax in the php-documentation on php.net?

I didn't get this notice on another computer where in August I installed the newest version of php and where on I wrote this code.

(The notice shows up on an old computer running an older php-version. Or I make a mistake with the current input data. Both could be the reason.)


Solution

  • Negative string indices were only introduced in PHP7.1 (see the manual, "negative numeric indices"). You can either upgrade your PHP version, or for lower versions, use the workaround:

    substr($string, -1, 1)
    

    Demo on 3v4l.org