Factorial Program in PHP: Examples Using Loops & Recursion

Introduction

In programming, mathematical concepts are often used to solve real-world problems. One such important concept is factorial. It is widely used in areas like mathematics, algorithms, probability, and data structures.

In PHP, calculating the factorial of a number is a common task that helps beginners understand loops, recursion, and function logic. Whether you are preparing for interviews or learning PHP basics, factorial programs are a must-know topic.

What is Factorial?

A factorial is the product of all positive integers less than or equal to a given number.

Note: It is denoted by ! (exclamation mark).

Example:


5! = 5 × 4 × 3 × 2 × 1 = 120
📖
Important Points:
  • Factorial is defined only for non-negative integers
  • 0! = 1 (by definition)
  • Factorial grows very fast as numbers increase

Why it is Used

Factorial is used in various fields of programming and mathematics:

1. Mathematical Calculations

Used in permutations, combinations, and probability.

2. Algorithm Design

Helpful in recursion and problem-solving techniques.

3. Data Structures

Used in tree and graph-related problems.

4. Interview Questions

Commonly asked in coding interviews.

5. Learning Recursion

Best example to understand recursion concepts.

Syntax (General Logic)

There is no built-in PHP function for factorial, so we implement it manually using:

  • Loops (for, while)
  • Recursion (function calling itself)

Examples:

1. Factorial Program Using for Loop


<?php
function factorial($n) {
   $fact = 1;
   for($i = $n; $i >= 1; $i--) {
       $fact = $fact * $i;
   }

   return $fact;
}
echo factorial(4); // Output: 24
echo factorial(5); // Output: 120
?>

Explanation:

  • Start from the given number
  • Multiply each number until 1
  • Store result in $fact

2. Factorial Using while Loop


<?php
function factorialWhile($n) {
   $fact = 1;
   while($n >= 1) {
       $fact *= $n;
       $n--;
   }

   return $fact;
}
echo factorialWhile(5); // Output: 120
?>

3. Factorial Using Recursion


<?php
function recursiveFactorial($number) {
   if($number == 0) {
       return 1;
   }
   return $number * recursiveFactorial($number - 1);
}
echo recursiveFactorial(4); // Output: 24
echo recursiveFactorial(5); // Output: 120
?>

Explanation:

  • Function calls itself
  • Base condition: 0! = 1
  • Recursively multiplies numbers

4. Factorial with User Input


<?php
$number = 5;
$fact = 1;

for($i = 1; $i <= $number; $i++) {
   $fact *= $i;
}
echo "Factorial of $number is $fact";
?>

5. Handling Invalid Input


<?php
function factorialSafe($n) {
   if($n < 0) {
       return "Factorial not defined for negative numbers";
   }
   $fact = 1;
   for($i = 1; $i <= $n; $i++) {
       $fact *= $i;
   }
   return $fact;
}
echo factorialSafe(-3);
?>

Real-Life Example

Scenario 1 : Calculating Permutations

Factorial is widely used in permutation formulas:


nPr = n! / (n - r)!

Example in PHP:


<?php
function factorial($n) {
   $fact = 1;
   for($i = 1; $i <= $n; $i++) {
       $fact *= $i;
   }
   return $fact;
}
$n = 5;
$r = 2;
$permutation = factorial($n) / factorial($n - $r);
echo "Permutation is: " . $permutation;
?>

Scenario 2: Arranging Items

If you have 4 items and want to know how many ways you can arrange them:

4! = 24 ways

Note: Used in scheduling, arrangements, and ordering systems.

Common Mistakes

1. Ignoring Base Case in Recursion


if($number == 0)

Note: Without this, recursion will never stop.

2. Using Negative Numbers

Factorial is not defined for negative values.

3. Integer Overflow

Large numbers can exceed PHP integer limits.

4. Incorrect Loop Condition


for($i = 0; $i <= $n; $i++)

Note: Multiplying with 0 gives the wrong result.

5. Forgetting Initialization


$fact = 0;

Note: Should start from 1.

Conclusion

The factorial program in PHP is a fundamental concept that helps you understand loops, recursion, and mathematical logic. It is widely used in problem-solving, algorithms, and real-world applications.

By learning factorial programs, you can:

  • Improve logical thinking
  • Understand recursion deeply
  • Solve complex mathematical problems