164 of 410 menu

The array_push Function

The array_push function adds elements to the end of an array. The passed array is modified, and the function returns the new number of elements in the array. Elements to be added are listed separated by commas.

Syntax

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

Example

Let's add elements to the end of an array:

<?php $arr = [1, 2, 3]; $num = array_push($arr, 4, 5); var_dump($arr); ?>

Code execution result:

[1, 2, 3, 4, 5]

The variable $num will contain the new number of array elements:

5

See Also

  • the array_pop function,
    which removes the last element of an array
byenru