Javatpoint Logo
Javatpoint Logo

PHP array_push

The array_push() is a built-in function of PHP. This function helps the users to add the elements at the end of the array. It allows to insert any number of elements in an array. Even you can add a string as well as numeric values. The length of the array increases whenever an element adds or pushes into the array,.

Note - if your array has string values, those added elements have numeric keys always.

The array_push() methods It is almost similar to the array() function and has the same effect. It treats an array as a stack..

Syntax

The syntax of array_push() is as follows:

This function has one or more parameters in which one is permanent and the other are optional parameters. These parameters are as follows with a brief description -

array (required) - It is an array type of parameter. It is must to pass this parameter in this function. This array parameter can contain any type of value, either string or numeric.

value1 (required/optional) - Before PHP 7.3, it was a required parameter, which must have to be passed in this function. After PHP version 7.3, it became an optional parameter now. This parameter contains the value to be pushed into the array.

value2 (optional) - It is an optional parameter, which is not required to pass in this function. This parameter also contains the value to be pushed into the array.

Return Value

The array_push() function returns number of elements in array. It returns all the elements present in the array after they have pushed in it.

Example 1

Below is a simple example of array_push() function in which we will initialize an array containing three elements in it. We will push two more elements in it.

Execute the below code on your server.

Output

In this below output, you can see that elements are added at the end of the array and default indexing is provided to it, which starts from 0.

Array ([0] => Rolex [1] => Fastrack [2] => Titan [3] => Fossil [4] => Omega)

Screenshot

PHP array_push

Example 2

In this example, we will initialize an array containing three elements in it along with the memory index. Now, we will push two more elements in it, which will start taking index from 0.

Execute the below code on your server.

Output

In this below output, you can see that two elements are added at the end of the array with indexing 0 and then 1.

Array ([a] => Rolex [b] => Fastrack [c] => Titan [0] => Fossil [1] => Omega)

Screenshot

PHP array_push

Example 3

In this example, we will not pass the optional values to array_push() to push it into the array. Execute the below code on your server.

Output

This will return the array values as it is and display them on them without occurring any error.

Array ([0] => Football [1] => Cricket [2] => Hockey)

Screenshot

PHP array_push
Next Topic#




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA