Programming Tips - PHP: append an element to the end of a PHP array

Date: 2009nov26 Update: 2025jun25 Language: php Q. PHP: append an element to the end of a PHP array A. Assign to [] like this:
$a = array(); $a[] = 1; $a[] = 2; $a[] = 3; print_r($a);
This is a nice / compact syntax but not immediately guessable. This will output:
Array ( [0] => 1 [1] => 2 [2] => 3 )
If you want to add multiple elements, use array_push(). For example:
array_push($a, 100, 200, 300);