Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Array Append

Append, or array append, is an array operation, which adds an element at the end of the array. It is also called a push.

There are two operators for this feature ; the [] operator, which has to be placed after a variable or member. There is also the array_push() function, which is available as a callback, unlike the operator.

The opposite operation is a pop, with the function array_pop(). array_unshift() is the append at the beginning of the array.

<?php

    $array = [1, 2, 3];
    $array[] = 4;
    
    $array []= 5; // Same as above, with a notation similar to a short assignation
    
    array_push($array, 6);
    
    // $array === [1, 2, 3, 4, 5, 6];

?>

Documentation

See Also