PHP:Accumulate Adding and Counting

From Progzoo
Jump to: navigation, search

Using an Accumulator

Using an accumulator to add.

The variable list is an array, it holds several integers. For example it might be the list (1,2,3,4).

You can iterate over a list, every time you visit an element of the list you do something to the accumulator $acc

In this example, for every member $i of the list you add that member to the accumulator using the code

$acc = $acc + $i;


[Font] [Default] [Show] [Resize] [History] [Profile]

Using an accumulator to count.

Use the accumulator to count the number of items in the list:


[Font] [Default] [Show] [Resize] [History] [Profile]

Using an accumulator to multiply.

Use the accumulator to find the product of items in the list:


[Font] [Default] [Show] [Resize] [History] [Profile]

Using an accumulator to max.

Use the accumulator to find the largest item in the list:


[Font] [Default] [Show] [Resize] [History] [Profile]