Math average with php

Marcus picture Marcus · Jan 10, 2010 · Viewed 75.8k times · Source

Time to test your math skills...

I'm using php to find the average of $num1, $num2, $num3 and so on; upto an unset amount of numbers. It then saves that average to a database.

Next time the php script is called a new number is added to the mix.

Is there a math (most likely algebra) equation that I can use to find the average of the original numbers with the new number included. Or do I need to save the original numbers in the database so I can query them and re-calculate the entire bunch of numbers together?

Answer

Isra picture Isra · Nov 16, 2010
array_sum($values) / count($values)