0 points

PHP Master | Understanding Recursion


dashboard

Wed Dec 02 2020 05:05:12 GMT+0000 (UTC)

Posted by @mvieira #none

<?php
function factorial ($number, $factorial = 1) {
    if ($number < 0) {
        throw new InvalidArgumentException('Number cannot be less than zero (0)');
    }
    if ($number == 0) {
        return $factorial;
    }
    else {
        return factorial($number - 1, $factorial * $number);
    }
}
content_copy Copy

https://www.sitepoint.com/understanding-recursion/