How can one add elements to an array in PHP?
In PHP, you can add elements to the end of an array using the array_push() function or simply using the [] syntax. Here is an example code:
// 使用array_push()函数添加元素
$fruits = array("apple", "banana", "orange");
array_push($fruits, "grape");
print_r($fruits); // 输出:Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
// 使用[]语法添加元素
$fruits = ["apple", "banana", "orange"];
$fruits[] = "grape";
print_r($fruits); // 输出:Array ( [0] => apple [1] => banana [2] => orange [3] => grape )
Whether you use the array_push() function or the [] syntax, you can add elements to the end of an array.