To find the sum of a list of numbers in PHP, you can use a loop to iterate through the array and add each element to a running total. Here's an example:
main.php139 chars10 lines
In this code, we define an array of numbers as $numbers
. We then initialize a variable $sum
to 0, which will keep track of the running total.
We use a foreach
loop to iterate through each element in the array, adding the current element to $sum
using the addition operator (+=
).
Finally, we output the sum using echo
.
When this code is run, it will output:
main.php30 chars2 lines
gistlibby LogSnag