<?php
function calculateFactorial($n) {
if ($n == 0 || $n == 1) {
return 1;
}
return $n * calculateFactorial($n - 1);
}
$number = 5;
$factorial = calculateFactorial($number);
echo "Factorial of $number is: $factorial";
?>
- The script defines a recursive function
calculateFactorial
to compute the factorial of a given number. - The base case checks if the number is 0 or 1, in which case the factorial is 1.
- The recursive case multiplies the number by the factorial of the number minus 1.
- The script then calls the function with a sample number (
$number
) and prints the result.