String Offset Curly Brace Syntax
debt(d3/e3/b3/t5)
Closest to 'default linter catches the common case' (d3). The detection_hints list rector, phpstan, and phpcs — all widely-used default-tier tools in PHP CI pipelines. The code_pattern notes the $str{0} curly brace usage is straightforwardly grep-able and caught automatically by these tools, placing it firmly at d3 rather than d5 (specialist) since phpcs and phpstan are standard default tooling in most PHP projects.
Closest to 'simple parameterised fix (replace pattern with safer alternative)' (e3). The quick_fix and common_mistakes both confirm the fix is a mechanical replacement: $str{0} → $str[0] across the codebase. Rector's PHP80MigrationRector set automates this entirely. While it may touch multiple files, each change is a trivial one-line substitution with no logic change, making it closer to e3 than e5.
Closest to 'localised tax' (b3). The syntax applies across web, cli, and queue-worker contexts (broad applies_to), but once identified and replaced it imposes no ongoing structural weight. Its burden is felt during the PHP 8 migration audit window only — it doesn't shape architecture or slow future work streams after remediation, keeping it at b3 rather than b5.
Closest to 'notable trap (a documented gotcha most devs eventually learn)' (t5). The misconception field confirms the trap: developers reading pre-7.4 tutorials or Stack Overflow answers believe curly brace syntax is a valid alternative, not realising it was deprecated in 7.4 and is a fatal error in PHP 8.0+. This is a well-documented gotcha that catches developers during PHP 8 upgrades, but it is not a deep behavioural contradiction — it is a removed syntax — so t5 fits rather than t7.
Also Known As
TL;DR
Explanation
PHP historically allowed both $str[0] and $str{0} to access individual characters in a string by index. The curly brace syntax $str{n} was deprecated in PHP 7.4 with a deprecation notice and fully removed in PHP 8.0 — it now produces a fatal parse error. The fix is trivial: replace {} with []. PHPStan, Rector, and PHP-CS-Fixer all detect and auto-fix this. It is purely a legacy pattern with no advantage over square brackets.
Common Misconception
Why It Matters
Common Mistakes
- $str{0} for first character — use $str[0].
- $str{strlen($str)-1} for last character — use $str[-1] (negative indexing, PHP 7.1+) or $str[strlen($str)-1].
- Not running a codebase-wide search before PHP 8 upgrade.
- Rector's PHP80MigrationRector set fixes this automatically.
Code Examples
// Deprecated PHP 7.4, fatal in PHP 8.0+:
$first = $str{0}; // Fatal error in PHP 8
$last = $str{strlen($str)-1}; // Fatal error in PHP 8
if ($str{0} === '/') { // Fatal error in PHP 8
// ...
}
// Square brackets — always correct:
$first = $str[0];
$last = $str[-1]; // Negative indexing (PHP 7.1+)
$last = $str[strlen($str)-1]; // Or explicit
if ($str[0] === '/') {
// ...
}
// Auto-fix with Rector:
// vendor/bin/rector process src --config rector.php
// Uses: \Rector\Php80\Rector\Array_\StringableToStringRector