array_append() And Short Assignation
<?php
$x = [];
$x[] = 1;
$x[] += 2;
$x[] ??= 3;
$x[] *= 4;
$x[] **= 5;
$x[] |= 6;
print_r($x);
?>
PHP allows using short assignation operators with the array append operators. This means that the code adds 2 to the array append. No error is displayed, except in the case of ??=, who is yield a Fatal error, with an explicit “Cannot use [] for reading”.
In any case, all of those expressions make little sense : the short assignation operators shall exist on an existing value, not a new one. Here, it looks like the default value is null.
See Also
- Array (PHP manual)
- Run this code [Try me]
PHP Error Messages
PHP Features
- operator
- array
- append
- error
- case
- yield
- explicit
- reading
- expression
- value
- not
- new
- default-value
- null
- short-assignation
Last updated: 09 November 2026