Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

array_push

Appends elements to the end of an array.

Syntax

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

Parameters

ParameterTypeDescription
$arrayarrayThe array being modified.
...$valuesmixedThe values to append.

Return value

intThe new number of elements.

Examples

Several values at once:

<?php
 
$colors = ['red'];
array_push($colors, 'green', 'blue');
 
echo implode(', ', $colors);

Output:

red, green, blue

For a single value the bracket form is simpler:

<?php
 
$colors = ['red'];
$colors[] = 'green';
 
echo count($colors);

Output:

2

Notes

The function modifies the array itself. $array[] = $value does the same and is faster.

See also

  • array_pop — Removes the last element of an array and returns it.
  • array_unshift — Prepends elements to the beginning of an array.

In the tutorial

  • Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.

Thanks for reading!

Donate to the site — we work hard for you.