Home > Backend Development > PHP Tutorial > How to use php recursive function to accumulate numbers? PHP recursive function cumulative sum

How to use php recursive function to accumulate numbers? PHP recursive function cumulative sum

WBOY
Release: 2016-07-25 08:51:20
Original
1957 people have browsed it
Regarding the method of php recursive function to implement the accumulation of numbers, a small example of php recursive accumulation and summing of numbers, and the method of php recursive function to return the correct accumulated variable.

1. PHP recursive function to realize number accumulation

Example:

Copy code Code example:

2. PHP recursive function cumulative summation

php Example of array recursive summation: convert an array, or a two-dimensional array of multiple numbers and return the sum of all values.

Example:

Copy code Code example:

/*** a flat array ***/ $array = array(10, 20, 5, 34, 129);

/*** add the values ***/ echo arraySumRecursive($array)."
";

/*** a multi dimensional array ***/ $array = array(10, 20, 5, array(5, 2, 3, array(5, 3, array(2, 10, array(19, 1) ),3 ), 2, 7 ), 3 );

/*** add the values ***/ echo arraySumRecursive($array); ?>



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template