W3docs

PHP How to add to end of array?

In PHP, you can add an element to the end of an array using the array_push() function.

In PHP, you can add an element to the end of an array using the array_push() function. The function takes the array as its first argument and the element to add as its second argument. Here's an example:

Example of adding to end of an array with array_push() function in PHP

php— editable, runs on the server
Info

Watch a video course: Learn object oriented PHP

You can also use the short array syntax, and the append [] operator.

Example of adding to end of an array with short array syntax in PHP

php— editable, runs on the server

After these examples, the $fruits array will now contain the elements: "apple", "banana", "cherry", "orange".