Adding Sum of Values of an Array with array_sum

Hello all! It has been awhile. So this morning I decided to do some code exercises on Hacker Rank (which is an awesome site by the way). I came across an exercise that wanted me to add up all of the values in an array. I thought to myself, “easy enough!” There’s a simple built in method in Python, “sum” which of course, gets the sum values. I began to wonder if there was a similar method in PHP. I came across array_sum().

Let’s say you have an array called “foo”. You can add up the values in that array.

<?php

 $foo = [50, 10, 30, 40];
 echo array_sum($foo); // output is 130

You can find the array_sum documentation on the official php.net site: http://php.net/manual/en/function.array-sum.php

Tyler Souza

Tyler is a very passionate full-stack developer who thrives on a challenge. He specializes in programming (mainly in Python), REST API development, and keeps up with the latest front-end technologies. When not coding, he loves to eat ramen, BBQ, and travel.

You may also like...