function factorial($n) {
        if ($n === 0 || $n === 1) {
            return 1;
        } else {
            return $n * factorial($n - 1);
        }
    }

// Calculate and display the factorial of a number
    $num = 5; // Number for which factorial needs to be calculated

    $factorial = factorial($num);

    echo "Factorial of $num is: $factorial";

In the above example, the factorial() function takes the number n as a parameter and recursively calculates the factorial of that number. If n is 0 or 1, it returns 1 as the base case. Otherwise, it multiplies n with the factorial of n-1 to calculate the factorial.

Finally, the calculated factorial is displayed using echo.

When you run the above code with $num = 5, it will calculate and display the factorial of 5, which is 120.

You can change the value of $num to calculate the factorial for a different number.

Leave a Reply

Your email address will not be published. Required fields are marked *