Geometric mean ( Two Methods ) | function to calculate geometric mean and return float value . ; declare product variable and initialize it to 1. ; Compute the product of all the elements in the array . ; compute geometric mean through formula pow ( product , 1 / n ) and return the value to main function . ; Driver Code
< ? php function geometricMean ( $ arr , $ n ) { $ product = 1 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) $ product = $ product * $ arr [ $ i ] ; $ gm = pow ( $ product , ( float ) ( 1 / $ n ) ) ; return $ gm ; } $ arr = array ( 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ) ; $ n = sizeof ( $ arr ) ; echo ( geometricMean ( $ arr , $ n ) ) ; ? >