PHP array push function with an example

PHP Array Push function

The array push function, as the name shows, pushes one or more elements at the end of an array.

Syntax of array push

This is how you may use the array push function:

array_push($Arr_name,[value1],[value2])

The array push PHP function pushes the passed value at to the end of an array and treats the array as a stack.

Array_push has the same effect as:



repeated for each variable.

An example of array push 

Following is an array push function example where we will push two array elements in an array. After that, we will print array by using the print_r function of PHP.

PHP array push

Experience this example online



The print_r would output human readable $fruits array as below:



 

Tip: The Array_push is useful for adding more than one element. If you just need to add one element then it is better to use $array[] = to avoid function calling.

 

Further reading: PHP Array


Was this article helpful?

Related Articles

Leave A Comment?