PHP | Factorial of a number
Last Updated :
14 May, 2025
In mathematics, the factorial of a number is the product of all positive integers less than or equal to that number. It is denoted by n!. The factorial of a number can be expressed mathematically as:
n! = n * (n-1) * (n-2) * ........ * 1
Methods to Calculate Factorials in PHP
There are several methods to calculate the factorial of a number in PHP. Let’s explore some of the most common approaches.
1. Using a Loop
One of the most straightforward ways to calculate the factorial of a number is by using a loop. We will multiply all integers from 1 to the given number.
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
$result = 1;
for ($i = 1; $i <= $n; $i++) {
$result *= $i;
}
return $result;
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
OutputFactorial of 5 is 120
In this example:
- We define a function
factorial($n)
that calculates the factorial of the number. - The loop runs from 1 to
$n
, multiplying each number with $result
to accumulate the product. - Finally, the result is returned.
2. Using Recursion
Recursion is another elegant way to calculate the factorial of a number. In this method, the function calls itself, reducing the number until it reaches the base case (0! = 1
).
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
if ($n == 0) {
return 1;
}
return $n * factorial($n - 1);
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
OutputFactorial of 5 is 120
In this example:
- The base case for recursion is when
$n
is 0
. In that case, the factorial is 1
. - The recursive case multiplies
$n
by the factorial of n-1
. - The function keeps calling itself until the base case is reached.
3. Using PHP’s Built-In gmp_fact()
Function
If you need to calculate the factorial of very large numbers, PHP provides the gmp_fact()
function in the GMP (GNU Multiple Precision) library, which handles large integers more efficiently.
Now, let us understand with the help of the example:
PHP
<?php
$factorial = gmp_fact(5);
echo "Factorial of 5 is " . gmp_strval($factorial); // Output: Factorial of 5 is 120
?>
In this example:
- The
gmp_fact()
function calculates the factorial of the number, and gmp_strval()
converts the result into a string for easy display. - This method is highly efficient for very large numbers because it supports arbitrary-precision arithmetic.
4. Using bcMath
Functions for Large Numbers
For extremely large numbers, PHP's bcMath
library can be used to handle arbitrary-precision numbers. The bcMul()
function can be used to calculate the factorial by multiplying the numbers.
Now, let us understand with the help of the example:
PHP
<?php
function factorial($n) {
$result = '1'; // bcMath functions work with strings
for ($i = 2; $i <= $n; $i++) {
$result = bcmul($result, (string)$i); // Multiply result by i
}
return $result;
}
echo "Factorial of 5 is " . factorial(5); // Output: Factorial of 5 is 120
?>
In this example:
- The
bcmul()
function multiplies two numbers (in string format) and returns the result as a string, which helps to avoid overflow issues when dealing with large integers. - This approach is helpful when dealing with very large numbers that exceed PHP’s default integer size.
Conclusion
In this article, we explored different methods to calculate the factorial of a number in PHP. Whether you choose to use a loop, recursion, built-in functions like gmp_fact()
, or even libraries like bcMath
, the choice depends on the size of the numbers and the requirements of your application.