Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a better understanding on how to declare and how to use PHP array in PHP coding.
Array elements can be added manually or using PHP built-in array functions. It is so easy to add PHP array elements with PHP built-in function. So, this article I am going to show how to add PHP array elements using built-in array functions.

Adding Element at the End of a PHP Array
PHP has thousands of built-in functions for various purposes. Among them, the array_push() function is used to add one or more elements at the end of an array. The structure of a PHP array_push() function is:
array_push($array_name,”Element to add”);
The first argument of the array_push() function is an array where new elements will be added and other arguments will be desired array elements those will be inserted. If the provided array already contains any element, the new elements will be added at the end of that array.
See the following example where the array_push() function is being used to add elements at the end of a PHP array.
<?php
$web = array("PHP","HTML","CSS");
array_push($web,"JavaScript","AJAX");
foreach($web as $lang){
echo $lang." ";
}
?>
Output: PHP HTML CSS JavaScript AJAX
Here, two new elements has been added at the end of the $web array using the array_push() function and then the array elements are being retrieved using PHP foreach loop statement.
Add Element at the beginning of an Array
Besides adding array element at the end, it is also possible to add element at the beginning of a PHP array using PHP built-in function. The array_unshift() function is a built-in function which is used to add elements at the beginning of a PHP array. The structure of the array_unshift() function is:.
array_unshift($array_name,”Element to add”);
Like the array_push() function, the first argument of the array_unshift() function must be an array where new elements will be inserted and other arguments will be desired new array elements those will be added at the beginning of this array.
See the following example where the array_unshift() function is being used to add elements at the beginning of a PHP array.
<?php
$web = array("PHP","HTML","CSS");
array_unshift($web,"JavaScript","AJAX");
foreach($web as $lang){
echo $lang." ";
}
?>
Output: JavaScript AJAX PHP HTML CSS
Here, two new elements has been added at the beginning of the $web array using the array_unshift() built-in function and then array elements are being retrieved using PHP foreach loop statement.
How to add array elements at the beginning or at the end of an array using PHP built-in array function has been explained in this article. I hope you will now be able to add PHP array elements using PHP built-in function so easily. However, if you have any confusion on PHP array_push() or array_unshift() function, feel free to discuss in comment or contact me from Contact page. I will try my best to stay with you.