0
0
PhpProgramBeginner · 2 min read

PHP Program to Find Factorial of a Number

You can find the factorial of a number in PHP using a loop like for ($i = 1; $i <= $n; $i++) { $factorial *= $i; } to multiply all numbers from 1 to $n.
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input1
OutputFactorial of 1 is 1
🧠

How to Think About It

To find the factorial of a number, multiply all whole numbers from 1 up to that number. Start with 1 because multiplying by 1 does not change the value. Keep multiplying each number until you reach the input number.
📐

Algorithm

1
Get the input number.
2
Set a variable to 1 to hold the factorial result.
3
Loop from 1 to the input number.
4
Multiply the result variable by the current loop number each time.
5
After the loop ends, return or print the factorial result.
💻

Code

php
<?php
$n = 5;
$factorial = 1;
for ($i = 1; $i <= $n; $i++) {
    $factorial *= $i;
}
echo "Factorial of $n is $factorial";
?>
Output
Factorial of 5 is 120
🔍

Dry Run

Let's trace the factorial calculation for input 5 through the code.

1

Initialize variables

$n = 5, $factorial = 1

2

First loop iteration

$i = 1, $factorial = 1 * 1 = 1

3

Second loop iteration

$i = 2, $factorial = 1 * 2 = 2

4

Third loop iteration

$i = 3, $factorial = 2 * 3 = 6

5

Fourth loop iteration

$i = 4, $factorial = 6 * 4 = 24

6

Fifth loop iteration

$i = 5, $factorial = 24 * 5 = 120

7

Loop ends

Final factorial = 120

Iterationifactorial
111
222
336
4424
55120
💡

Why This Works

Step 1: Start with 1

We set $factorial = 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply in loop

Each loop multiplies $factorial by the current number $i, building the product step by step.

Step 3: Result after loop

After the loop finishes, $factorial holds the product of all numbers from 1 to $n, which is the factorial.

🔄

Alternative Approaches

Recursive function
php
<?php
function factorial($n) {
    if ($n <= 1) return 1;
    return $n * factorial($n - 1);
}
$n = 5;
echo "Factorial of $n is " . factorial($n);
?>
Uses function calling itself; elegant but can cause stack overflow for very large numbers.
Using while loop
php
<?php
$n = 5;
$factorial = 1;
$i = 1;
while ($i <= $n) {
    $factorial *= $i;
    $i++;
}
echo "Factorial of $n is $factorial";
?>
Same logic as for loop but uses while; good for beginners to understand loops.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so the time grows linearly with the input number.

Space Complexity

Only a few variables are used, so space stays constant regardless of input size.

Which Approach is Fastest?

Both loop and recursion have O(n) time, but loops use less memory and avoid function call overhead.

ApproachTimeSpaceBest For
For loopO(n)O(1)Simple and efficient for all inputs
RecursiveO(n)O(n)Elegant but risky for large inputs
While loopO(n)O(1)Alternative loop style, same efficiency
💡
Always start factorial calculation with 1 because multiplying by zero would give zero.
⚠️
A common mistake is starting multiplication from 0, which makes the factorial always zero.