To write a program that calculates the factorial of a number in PHP, you can use a recursive function or an iterative approach. Here’s a simple example using a loop:
<code class="language-php">function factorial($n) {
$result = 1; for ($i = 2; $i <= $n; $i++) { $result *= $i; } return $result; }
echo factorial(5); // Outputs: 120
</code>
This code defines a function that multiplies numbers from 2 up to the given number $n to compute the factorial.
Copyright © 2026 eLLeNow.com All Rights Reserved.