PHP 8 reference · Array Functions

PHP array_push() Function

Add one or more elements to the end of an array and return its new length. $array[] = $value does the same for one value.

PHP 8 Since 4.0

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_push(array &$array, mixed ...$values): int

Parameters

ParameterDescription
array The array to add to (changed in place).
values The values to append.

Return value

The new number of elements.

Examples

Append several valuesTry it yourself
<?php
$stack = ["a", "b"];
$length = array_push($stack, "c", "d");
echo $length, "\n";
print_r($stack);

Output

4
Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
)
The [] shortcutTry it yourself
<?php
$cart = [];
$cart[] = "tea";   // the usual way to add one value
$cart[] = "cake";
print_r($cart);

Output

Array
(
    [0] => tea
    [1] => cake
)

Version history

VersionChange
PHP 4.0Added
PHP 7.3Can be called with only the array argument.

In other languages

Was this page helpful?