Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP: array and string offset access with curly braces is deprecated

ErrorDeprecated: Array and string offset access syntax with curly braces is deprecated

Short answer

The form $str{0} was deprecated in PHP 7.4 and removed in PHP 8.0. Change every $var{$i} to $var[$i]. If the line sits in a vendor package such as an old PHPExcel, update or replace the package, because it will fail to parse on PHP 8.

  • Applies to PHP 7.4, PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Search your code and vendor folder for the old form. The first hit names the file to fix.

    grep -rnE '\$[A-Za-z_][A-Za-z0-9_]*\{[^}]+\}' --include=*.php app src vendor | head -20
  2. 2Match what you see, then jump to its fix

    1. 1The deprecation names a file inside your projectYour own code uses $var{0}Fix
    2. 2The file path starts with vendor/, for example phpoffice/phpexcelAn old library in vendor/ still uses itFix
    3. 3The script stops instead of printing a deprecationYou moved to PHP 8 and the old form now stops the scriptFix

What does the error look like?

  • Deprecated: Array and string offset access syntax with curly braces is deprecated

Cause 1 of 3

Your own code uses $var{0}

1Check

Open the file and line from the message.

php -l path/to/file.php

2Fix

Replace the braces with square brackets. The meaning is the same.

// before
$first = $str{0};
$item  = $list{$i};
// after
$first = $str[0];
$item  = $list[$i];

3Verify

The linter passes on the new PHP version.

php -l path/to/file.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

An old library in vendor/ still uses it

1Check

Find which package owns the file and what version is installed.

composer why phpoffice/phpexcel; composer show phpoffice/phpexcel

2Fix

Update the package to a release that supports your PHP version, or replace it with a maintained alternative. Do not edit files in vendor/, the change is lost at the next install. The PHP 8.0 guide lists the removal as a breaking change, so old releases cannot be run unmodified.

composer update vendor/package

3Verify

Run the code path that loads the library.

php artisan about

Confirmed by php.net

Did this fix it?

Cause 3 of 3

You moved to PHP 8 and the old form now stops the script

1Check

Confirm the PHP version the web server runs, which can differ from the CLI.

php -v

2Fix

On PHP 8 the syntax is gone, so only the source change works: convert every $var{...} to $var[...]. Running PHP 7.4 only hides it.

// before: $name{0}
// after:  $name[0]

3Verify

Lint every changed file.

find app -name '*.php' -print0 | xargs -0 -n1 php -l

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run php -l, or PHPStan, over the project on the PHP version you will deploy.
  • Check dependency support for the next PHP version before you upgrade.
  • Keep dependencies on maintained packages; abandoned ones block upgrades.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.