Backend Development 5 min read

Using PHP array_push() to Add Elements to an Array

This tutorial explains the PHP array_push() function, its syntax, parameters, return behavior, error handling, and provides clear code examples showing how to append single or multiple elements to an array, including an alternative bracket syntax.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP array_push() to Add Elements to an Array

In PHP, arrays are an important data type that allow us to store and organize multiple values. Sometimes we need to add new elements to an existing array. PHP provides the array_push() function to conveniently add elements to the end of an array. This article introduces the usage of array_push() and provides concrete code examples.

array_push() function syntax:

<code>array_push( $array, $element1, $element2, ... )</code>

The array_push() function accepts two or more arguments. The first argument is the array to which elements will be added, and the subsequent arguments are the elements to add. Multiple elements can be added at once, and they are appended to the array in the order given.

Below is a simple example demonstrating how to use array_push() to add elements to an array:

<code>$fruits = array("apple", "banana", "orange");
array_push($fruits, "grape", "watermelon");

print_r($fruits);
</code>

Output result:

<code>Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => grape
    [4] => watermelon
)
</code>

In the example, we first create an array containing three fruits. Then we use array_push() to add two new fruits, "grape" and "watermelon", to the array. Finally, print_r() prints the array, showing that the new fruits have been successfully added to the end.

Note that array_push() adds new elements to the end of the array and returns the new length of the array. If you only need to add elements and do not care about the returned length, you can ignore the return value. If the first argument passed to array_push() is not a valid array, a E_WARNING -level error will be thrown.

Besides array_push() , you can also use simple array syntax to append elements to the end of an array, achieving the same effect:

<code>$fruits = array("apple", "banana", "orange");
$fruits[] = "grape";
$fruits[] = "watermelon";

print_r($fruits);
</code>

The output and the result of using array_push() are identical.

The array_push() function is a very convenient way in PHP to add elements to the end of an array. Its usage is simple: provide the target array and the elements to add, listing multiple elements sequentially if needed. Alternatively, the bracket syntax can be used to achieve the same result, giving developers flexible options for managing array data.

PHP learning recommendations:

Vue3+Laravel8+Uniapp Beginner to Practical Development Tutorial

Vue3+TP6+API Social E‑commerce System Development Course

Swoole From Beginner to Master Course

Workerman+TP6 Real‑time Chat System Limited‑time Offer

Backend DevelopmentphpCode Examplearraysarray_push
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.