1.2.110. Cannot Use Append For Reading¶
The append operator []
is used to add a value to an array. It doesn’t provide an existing value to read. Hence, the short assignement operators, or the increment ones should not be used with the append operator. For example, the coalesce operator yields an error when used with append.
<?php
$x = [];
$x[] = 1; // normal usage
$x[] += 2; // adds a 2, but should yield an error
$x[]++; // adds a 1, but should yield an error
// variations with -= *= &= etc.
$x[] ??= 4; // yields a fatal error
?>