push array in php

In PHP, you can add an element to an array using the array_push() function or by simply assigning a new value to the next index in the array.

Using array_push():

main.php
$myArray = array(1, 2, 3, 4);
$newElement = 5;
array_push($myArray, $newElement);
print_r($myArray);
101 chars
5 lines

Direct assignment to the next index:

main.php
$myArray = array(1, 2, 3, 4);
$myArray[] = 5;
print_r($myArray);
65 chars
4 lines

Both of these methods will add the element '5' to the end of the array.

related categories

gistlibby LogSnag