What is the method to add elements to an array in PHP?

To add elements to an array in PHP, you can use the following method:

  1. “Use the bracket syntax”
$fruits = array('apple', 'banana', 'orange');
$fruits[] = 'kiwi';
  1. Use the array_push() function:
$fruits = array('apple', 'banana', 'orange');
array_push($fruits, 'kiwi');
  1. Directly specify key-value pairs in an array:
$fruits = array('apple', 'banana', 'orange');
$fruits['key'] = 'kiwi';

These methods can all be used to add new elements to an array.

bannerAds