← CodeClarityLab Home
Browse by Category
+ added · updated 7d
← Back to glossary

String Offset Curly Brace Syntax

php PHP 5.0+ Beginner

Also Known As

$str{0} curly brace string access string index curly

TL;DR

Using $str{0} to access string characters by index — deprecated since PHP 7.4 and removed in PHP 8.0; use square bracket syntax $str[0] instead.

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

Curly brace string access is an alternative valid syntax — it was an alternative in PHP 5-7.3, deprecated in 7.4, and is a fatal error in PHP 8.0+.

Why It Matters

Curly brace string offset syntax ($str{0}) was deprecated in PHP 7.4 and removed in PHP 8.0, throwing a fatal error. It appeared in older tutorials and Stack Overflow answers written before bracket syntax ($str[0]) was standardised. Codebases with legacy string manipulation code are the most likely to contain it, and grep-based detection is straightforward — making it a fast win during PHP 8 migration audits.

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

✗ Vulnerable
// 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
    // ...
}
✓ Fixed
// 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

Added 16 Mar 2026
Edited 23 Mar 2026
Views 18
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 1 ping W 0 pings T 0 pings F 0 pings S 2 pings S 0 pings M 1 ping T 0 pings W 0 pings T 0 pings F 0 pings S 1 ping S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 0 pings S 2 pings S 0 pings M 0 pings T 0 pings W 0 pings T 0 pings F 0 pings S
No pings yet today
No pings yesterday
Amazonbot 9 Perplexity 2 Google 1 ChatGPT 1 Ahrefs 1
crawler 14
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Low
⚡ Quick Fix
Use str_split($str) or mb_str_split($str) for character access — the $str[0] and $str{0} syntax accesses bytes not characters and is deprecated with curly braces in PHP 7.4
📦 Applies To
PHP 5.0+ web cli queue-worker
🔗 Prerequisites
🔍 Detection Hints
$str{0} curly brace string offset PHP 7.4+ deprecated; $str[0] on multibyte string accessing byte not character; string index access in loop for character processing
Auto-detectable: ✓ Yes rector phpstan phpcs
⚠ Related Problems
🤖 AI Agent
Confidence: High False Positives: Low ✓ Auto-fixable Fix: Low Context: Line

✓ schema.org compliant