How to find the sum of an array using PHP?

To calculate the sum of an array, you can use the array_sum() function in PHP. Here is an example:

<?php
// 定义一个数组
$numbers = array(1, 2, 3, 4, 5);

// 使用 array_sum() 函数计算数组的和
$sum = array_sum($numbers);

// 输出结果
echo "数组的和为: $sum";
?>

In this example, we start by defining an array $numbers that contains some numbers. Then, we use the array_sum() function to calculate the sum of this array and store the result in the variable $sum. Finally, we use an echo statement to display the calculated sum of the array.

bannerAds