What is PHP?
PHP is an open-source scripting language used for web development that can be embedded in HTML code. PHP is known for its flexibility and large developer community. PHP can run on many operating systems, including Windows, Linux, and macOS, and supports multiple databases such as MySQL, PostgreSQL, and SQLite.
What is an array in PHP?
In PHP, an array is a variable used to store a collection of data elements identified by a unique key or index (the key can be a string or a number). PHP has two types of arrays: indexed arrays and associative arrays. The PHP provides several built-in functions to manipulate arrays, such as sorting, searching, checking for element existence, splitting a string to array, converting arrays to string or JSON, and retrieving array length.
PHP Array Push Syntax
The following is the syntax of the array_push() function, which inserts one or more elements at the end of an array:
Where:
- array: defines an input array
- val (Optional): one or more elements that we want to put in the array. In the syntax above, the list looks like this: $val1, $val2, ….
PHP Array Push Examples
The following are examples of pushing an element into a PHP array:
Pushing one element into an array
The following is an example of pushing a single element into an array using the array_push() method:
Pushing multiple elements into an array
The following is an example of pushing multiple elements into an array using the array_push() method:
Pushing an element into a nested array
The following is an example of pushing an element into a nested array using the array_push() method: