How do you write a program to factorial in PHP?

1 answer

Answer

1174861

2026-07-11 09:15

+ Follow

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.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.