PHP Function Array_Sum
PHP is a powerful language that allows web developers to create dynamic websites with ease. One of its most useful functions is the
Array_sum() is an inbuilt function in PHP that takes an input array as its parameter and returns the sum of all elements present in it. The return value may be an integer or a float. Unlike other array functions, this function does not ignore non-numeric elements and instead, they are converted to numbers before being summed.
For example, let's say that you have an array of dates from a POST form and want to calculate their sum. Normally, you would create a foreach loop that goes through each date in the array and sums them up. However, you can do this much easier with a built-in native function.
In the code below, we remove the foreach loop and the working sum variable, replacing them with a call to the array_sum() function. This simple modification will solve our problem and provide the same result.
oXygen is the leading XML Editor and Developer Studio with support for XML Schema, DTD, Relax NG, XPath, XSLT, and more. Try it free for 30 days.