// Function to calculate factorial using recursion
function factorial($n) {
if ($n === 0) {
return 1;
}
return $n * factorial($n - 1);
}
// Usage: Calculate factorial of 5
$result = factorial(5);
code
Recursive Factorial Calculation in PHP
This PHP code defines a factorial function that calculates the factorial of a number using recursion. The function is then used to calculate the factorial of 5. The comments provide explanations for each part of the code.
Recursion
Factorial