Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The [] operator in PHP is a multi-purpose language construct used for array initialization (short array syntax), array element access, string offset access, element mutation, dynamic appending, symmetric destructuring, and object overloading via the ArrayAccess interface. It acts as a syntactic interface to PHP’s underlying hash table implementation (Zend Engine’s HashTable) for arrays, interacts directly with internal byte arrays for strings, and delegates to method calls for specific objects.

Array Initialization (Short Array Syntax)

Introduced in PHP 5.4, [] serves as a literal syntax for array creation, acting as a modern alternative to the older array() construct. It evaluates expressions internally and constructs the hash table in memory.
// Indexed array initialization
$indexed = [1, 2, 3];

// Associative array initialization
$associative = ['foo' => 'bar', 'baz' => 'qux'];

Element Access and Mutation

When appended to an array variable with a specified scalar key, the operator performs a hash lookup. In a read context, it returns the zval (Zend value) associated with the key. In a write context, it mutates the existing zval or allocates a new one if the key does not exist.
$array = ['foo' => 'initial_value'];

// Read context
$value = $array['foo'];

// Write/Mutation context
$array['foo'] = 'new_value';

String Offset Access

When applied to a string variable, the [] operator accesses or mutates individual bytes (characters) at a specified integer offset. This operation bypasses hash lookups entirely, interacting directly with PHP’s internal string byte arrays. Note that using the empty [] operator to dynamically append to a string is illegal in modern PHP and will throw a fatal Error.
$string = "PHP";

// Read byte at offset
$char = $string[0]; // 'P'

// Mutate byte at offset
$string[2] = 'X'; // "PHX"

// Illegal string append (Throws fatal Error)
$string[] = 'Y';

Dynamic Appending and Autovivification

When used as an assignment target without specifying a key ($var[] =), the operator triggers an append operation. The Zend Engine calculates the maximum existing integer key in the array, increments it by one, and assigns the new value to that index. If the array is empty or contains only string keys, the engine assigns the value to index 0. Crucially, this operator supports autovivification. If the append operator is applied to an undefined variable or a variable strictly set to null, PHP will automatically initialize that variable as a new array before performing the append operation.
$array = [10 => 'a'];
$array[] = 'b'; // Automatically assigned to index 11

// Autovivification from an undefined variable
$undefinedVar[] = 'c'; // Initializes as array, assigns 'c' to index 0

// Autovivification from null
$nullVar = null;
$nullVar[] = 'd'; // Initializes as array, assigns 'd' to index 0

Object Overloading via ArrayAccess

When the [] operator is applied to an object that implements the ArrayAccess interface, the Zend Engine intercepts the operation and delegates it to the object’s underlying interface methods. This allows objects to natively hook into the operator’s read, write, isset, and unset behaviors.
// Assuming $obj implements ArrayAccess

// Triggers $obj->offsetGet('key')
$value = $obj['key'];

// Triggers $obj->offsetSet('key', 'value')
$obj['key'] = 'value';

// Triggers $obj->offsetSet(null, 'value')
$obj[] = 'value';

// Triggers $obj->offsetExists('key')
isset($obj['key']);

// Triggers $obj->offsetUnset('key')
unset($obj['key']);

Symmetric Array Destructuring

As of PHP 7.1, the [] operator functions as a shorthand for the list() construct on the left side of an assignment. It unpacks an array, mapping its internal values to distinct variables based on index or explicit keys.
// Indexed destructuring
[$a, $b] = [1, 2];

// Associative destructuring
['id' => $id, 'name' => $name] = ['id' => 104, 'name' => 'System'];

Key Type Coercion Mechanics

When a value is passed inside the [] operator as an array key, PHP applies strict type casting rules before performing the hash lookup or allocation:
  • Strings containing valid decimal integers are cast to integers (e.g., $arr["8"] becomes $arr[8]).
  • Floats are truncated to integers (e.g., $arr[8.7] becomes $arr[8]).
  • Booleans are cast to integers (true becomes 1, false becomes 0).
  • Null is cast to an empty string ("").
  • Arrays and Objects can never be used as keys for standard arrays. Attempting to use an object as an array key will always result in a TypeError (or an Illegal offset type warning in older PHP versions), even if the object implements __toString() or the Stringable interface.
$array = [];
$array["10"] = "A";   // Cast to integer 10
$array[10.9] = "B";   // Truncated to integer 10, overwrites "A"
$array[true] = "C";   // Cast to integer 1
$array[null] = "D";   // Cast to string ""

// Throws TypeError
$array[new stdClass()] = "E"; 
Master PHP with Deep Grasping Methodology!Learn More