Cannot use [] for unsetting
Description
unset() removes a specific, existing element identified by an explicit key. The empty-bracket append syntax ($array[]) only has meaning in a write context, where it picks the next available integer key to create a new element; there is no such “next element” to remove, so combining the two is rejected at compile time.
Example
<?php
$array = [1, 2, 3];
unset($array[]);
?>
Alternatives
- Pass an explicit key to unset(), e.g. unset($array[2]);.
Related error messages
Changed Behavior
This error may appear following an evolution in behavior, in previous versions. See