Illegal String Offset

Warning: Illegal string offset 'name'

Quick Answer

You are using a string key to access a character in a string, but PHP expected an integer offset. The variable is a string, not an array.

Why This Happens

PHP allows accessing individual characters in a string using bracket notation with integer offsets like $str[0]. When you use a non-integer key like $str['name'], PHP raises this warning because strings only support integer offsets. This usually means the variable is a string where you expected an array.

The Problem

function processUser($user) {
    echo $user['name']; // $user is actually a string, not an array
}
processUser('Alice'); // Passing string instead of array

The Fix

function processUser(array $user): void {
    echo $user['name'];
}
processUser(['name' => 'Alice', 'email' => 'alice@example.com']);

Step-by-Step Fix

  1. 1

    Check the variable type

    Use var_dump() to inspect the variable. It is likely a string where you expected an associative array.

  2. 2

    Add type hints

    Add type hints to function parameters to catch type mismatches early. Use array or a specific class type.

  3. 3

    Fix the caller

    Ensure the calling code passes the correct type. If the function expects an array, pass an array, not a scalar value.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free