How to calculate factorials using recursion in PHP

13,003

Try this code, as suggested by LornaJane:

function factorial($number) {          
    if ($number < 2) {
        return 1;
    }
    return ($number * factorial($number-1)); 
}
Share:
13,003
h-s
Author by

h-s

Updated on July 18, 2022

Comments

  • h-s
    h-s almost 2 years

    I know this code to use in order to find the factorial of any number but what if we wanted it to do more and change up the code, so that it can calculate and print the factorials of number less that 100 recursively. How should this code be modified? Should i use for loop or some other technique?