Geometry Euclid The Sum Set

3-minute read

Calculate Sum of PHP Object with Single Line of Code

To calculate the sum of a PHP object, many tutorials show complex methods involving loops and addition. Can a simple one-liner achieve the same result? In many cases, yes!

PHP has a handy function for when you want to get the sum of values in an array: array_sum(). Unfortunately, there isn’t a counterpart object_sum() to calculate the values in an object. Just to be sure, I checked again today – maybe the PHP folks had added this functionality quietly along the way? Nope.

All I found were tutorials on how to manually calculate the sum of values in an object. There were loops and operators. It was a mess. Here’s a better way:

To calculate the sum of an object, cast it as an array and then use array_sum().

If you’re familiar with PHP and objects, the above statement will probably be enough – feel free to bail here. However, if you like practical examples, continue on.

Calculating the Sum of a PHP Object

Let’s say you had an object, $files, which held the totals for various files uploaded to your site. The object might consist of something like this:

You could loop over the object and add each value one-at-a-time, as shown in the following code block. At the end of the loop, $total_files would then hold the total number of files. This works, but, it involves 4 lines of code. We can do better.

The next code block shows a better approach to finding the sum of an object. It is simply to evaluate the object as an array with PHP’s typecasting! The following single line of code produces the same result as the four lines above.

As you can see, this is a much more elegant solution to finding the sum of an object. Less code means less places for bugs to hide, too! Now, you might be wondering, “Has my object been converted into an array now?” And, the answer is no. The object is only evaluated as an array in that single line – your original object remains intact.

A Few Final Notes

  • This technique works on the top-level of an object. Meaning that if you use it on a multidimensional object, it will only calculate the values that are held in the top-level of the object itself.
  • If you’re summing an object that contains values that are not integers or floats, those values will simply be ignored and only the numeric values will be summed.
  • This technique can be made to work with multidimensional objects with some tweaking.

Wrapping Up

PHP doesn’t offer a built-in mechanism to find the sum of an object, so, very often you will see loops and manual addition. This works, but, requires far more code than is needed. Instead, you can use PHP’s built-in ability to change the data type from object to array – and get your sum with just a single line of code.

What do you think?

Is this technique for summing an object new to you, or have you been doing it this way all along? Do you have any code that you can “lose” now that you know this method? Have you made it recursive? I’d love to hear your thoughts – let me know in the comments!

Leave a Reply

Your email address will not be published. Required fields are marked *

shares