PHP array_push() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_push() adds one or more values to the end of an array and returns the new number of items.

It changes the original array. For one item, the short form $array[] = $value does the same job.

Syntax

Call array_push() with the values you need. Start with this line.

PHP
$count = array_push($fruits, "banana", "cherry");
CallWhat it does
array_push($list, "a")Adds "a" to the end of $list.
array_push($list, "a", "b")Adds two values in one call.
$count = array_push($list, "c")Returns the new size of the array.

For adding a single value, $array[] = $value is a bit faster and is what most developers write.

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

Parameters

ParameterDescription
arrayRequired. The array to add to. It is passed by reference.
valuesOptional. One or more values to add at the end.

Return Value

Return ValueDescription
intThe new number of items in the array.

PHP Examples

Example 1 — Add Items to the End

Push two fruits onto a list and read the new size.

PHP
<?php
$fruits = ["apple"];

$count = array_push($fruits, "banana", "cherry");

echo implode(", ", $fruits), PHP_EOL;
echo $count, PHP_EOL;

How It Works

  1. Start with one fruit. The array holds apple.
  2. Push two values. banana and cherry go to the end.
  3. Read the return value. It is the new number of items, 3.
  4. Print the list. The original variable now has all three.

Example 2 — String Keys and Pushing an Array

New items get numeric keys, and an array is added as one item.

PHP
<?php
$a = ["x" => 1];
array_push($a, 2);
echo json_encode($a), PHP_EOL;

$n = [1];
array_push($n, [2, 3]);
echo count($n), PHP_EOL;

How It Works

  1. Start with a string key. The array has x set to 1.
  2. Push a value. The new item gets the numeric key 0.
  3. Push an array into a list. [2, 3] is added as a single item.
  4. Count the items. The list has 2 items, not 3.

Example 3 — array_push() vs the Short Form

Both ways add items to the end of an array.

PHP
<?php
$list = [1];

$list[] = 2;
$size = array_push($list, 3, 4);

echo implode(", ", $list), PHP_EOL;
echo $size, PHP_EOL;

How It Works

  1. Use the short form. $list[] = 2 appends one value.
  2. Use array_push(). It adds two more values in a single call.
  3. Read the return value. It is the size, 4.
  4. Print the list. Both methods built the same kind of array.

Frequently Asked Questions

It adds one or more values to the end of an array and returns the new number of items.
They both add to the end. The short form adds one value and has no function call. array_push() can add several values and returns the new count.
Use array_unshift(). It adds values at the start and renumbers the keys.
Use $array["key"] = $value. array_push() cannot choose a key.
Yes. The array is passed by reference, so the variable you pass is changed.

Next: array_rand()

array_rand() picks one or more random keys from an array.

array_rand() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful