PHP array indexing: $array[$index] vs $array["$index"] vs $array["{$index}"]

PHP array indexing: $array[$index] vs $array["$index"] vs $array["{$index}"]

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

PHP array indexing: $array[$index] vs $array["$index"] vs $array["{$index}"]

When writing dynamic code in PHP, the syntax for accessing array elements often feels like a trapdoor. A variable holding an index name seems straightforward, yet subtle variations in quoting can lead to unexpected behavior or performance implications. Specifically, developers frequently wonder if there is a functional difference between $array[$index], $array["$index"], and $array["{$index}"]. The answer depends entirely on the data type of $index and how PHP parses the expression before executing it.

Unquoted Variables are Literal Indices

The first form, $array[$index], is the standard way to access an array element using a variable for the key. In this context, PHP treats the contents of $index as the literal index to look up. If $index holds the integer 0, PHP resolves this to the first element. If $index holds the string "foo", it looks up the key "foo". This is the expected behavior for dynamic indexing and covers 95% of use cases involving associative arrays or numeric sequences.

Single Quotes Create a Literal String

The second form, $array["$index"], introduces a critical change in parsing logic. By wrapping the variable in double quotes inside the brackets, you are not interpolating the value of $index; instead, you are creating a literal string key named "$index".

Consider this example where $index contains 0:

$index = 0;
$array[1] = 'value';
echo $array["$index"]; // Outputs: "" (empty string)

Here, PHP does not look up index 0. It looks for a key literally named "$index". Since that key likely doesn't exist, you get an empty result or a notice depending on your error reporting settings. This syntax is functionally identical to $array['"$index"'] and should generally be avoided unless you are explicitly constructing a key that includes quotes as part of the name itself.

Double Quotes Enable Interpolation

The third form, $array["{$index}"], returns to interpolation but with specific scoping rules. Inside double quotes within the bracket expression, PHP attempts to interpolate variables. However, there is a nuance regarding variable scope. If $index is defined in the current scope, it works exactly like $array[$index].

However, if you are inside a closure or a function and only have access to global variables, this syntax might fail without declaring them as global first. More importantly, this syntax offers no performance advantage over the unquoted version. The parser still has to resolve the string concatenation/interpolation step before accessing the hash table, making it slightly more expensive in tight loops compared to the direct variable reference of $array[$index].

Practical Implications for Dynamic Keys

The real utility of these variations appears when dealing with complex keys or when mixing types. For instance, if you need a numeric key but want to ensure it is treated as a string (useful when iterating over arrays that mix types), the unquoted variable [$index] will automatically cast integers to strings for lookup purposes in most contexts, preserving the hash table structure.

If your application relies heavily on dynamic keys derived from user input or database fields, always default to $array[$index]. It is the most readable and performant option. Using $array["$index"] usually indicates a misunderstanding of string interpolation rules unless you are deliberately trying to access a key that starts with a dollar sign or includes quotes in its name. Similarly, while $array["{$index}"] works for simple cases, it adds unnecessary parsing overhead inside high-frequency loops and can obscure scope issues if the variable is not globally accessible.

Understanding these distinctions prevents subtle bugs where dynamic indices fail silently because they are being treated as literal strings rather than values to be resolved.

Our network