PHP: array and string offset access with curly braces is deprecated
Deprecated: Array and string offset access syntax with curly braces is deprecatedShort 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.
Fix it fast
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 -202Match what you see, then jump to its fix
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.php2Fix
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.phpConfirmed by php.net
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/phpexcel2Fix
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/package3Verify
Run the code path that loads the library.
php artisan aboutConfirmed by php.net
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 -v2Fix
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 -lConfirmed by php.net
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
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix