Factorial calculation is really cool thing. In mathematics the factorial calculation follows nice rules. In PHP the factorial can be calculated in different ways. Lets first consider recursive way of finding out factorial of a number.
5! = 5 x 4 x 3 x 2 x 1
5! = 120
function getFactorial($num){
static $total = 1;
$total = $total * $num--;
return ($num > 0) ? getFactorial($num) : $total;
}
echo "The factorial of 5 is: " . getFactorial(5);
The factorial of 5 is: 120
Comments
Post a Comment