# 19+ PHP Factorial Calculator Examples

This post contains a total of 19+ Hand-Picked PHP Factorial Calculator Examples with Source Code. All the Factorial Calculators are made using PHP Programming language.

You can use the source code of these programs for educational purpose with credits to the original owner.

### Related Posts

Click a Code to Copy it.

## 1. By πβ’β¦ββ£βπ

Made by πβ’β¦ββ£βπ. A basic PHP program to find the factorial of a number. Input number in “\$num = 4;”( Source )

``````<?php

\$num = 4;
\$factorial = 1;
for (\$x=\$num; \$x>=1; \$x--)
{
\$factorial = \$factorial * \$x;
}
echo "Factorial of \$num is \$factorial";

?>

/*
Output

Factorial of 4 is 24``````

## 2. By Michael Ngwerume

Made by Michael Ngwerume. Simple Program to convert a number to its factorial. You will need to enter your number at  echo f(5);. ( Source )

``````<?php
function f(\$i){
if(\$i==0||\$i==1)
return 1;
return \$i*f(\$i-1);
}
echo f(5);
?>``````

## 3. By Ξ±βΞΉΡΡzΞ±

Made by Ξ±βΞΉΡΡzΞ±. Enter a integer in \$a = 76 ; to get its factorial. ( Source )

``````<?php
function factorial(&\$fact){

\$result = \$fact * (\$fact - 1);
\$fact -= 1 ;
while ( \$fact > 1 ){
\$result = \$result * (\$fact - 1);
\$fact = \$fact - 1;
}
\$fact = \$result ;
return \$fact;
}

\$a = 76 ;
factorial(\$a);
echo \$a;

?>
``````

## 4. By Meet Soni

Made by Meet Soni. Change \$n to your input number. ( Source )

``````<?php
\$i=1;
\$n=4;
\$sum=1;
while(\$i<=\$n)
{
\$sum=\$sum*\$i;
\$i++;
}
echo"factorial is \$sum";
?>``````

## 5. By pamino

Made by pamino. ( Source )

``````<?php

\$factorial = 1;

for(\$counter=5; \$counter>=1; \$counter--)
{
\$factorial *= \$counter;
}

echo \$factorial; // 5*4*3*2*1 = 120

?>``````

## 6. By anne_stark

Made by anne_stark. ( Source )

``````<?php

\$num = 5;
\$factorial = 1;

echo "<br><br>" . \$num . " = " ;

for (\$count = \$num; \$count >= 1; \$count--){

\$factorial = \$factorial * \$count;  if(\$count > 1){

echo \$count . " * ";

}

}

echo '1';

?>
``````

## 7. By Faisal

Made by Faisal. Remove ‘rand(0,100)’ and enter the number you want to get the factorial of. ( Source )

``````<?php
\$numthing = rand(0,100);
\$thing = 0;
\$result = 1;

if(\$numthing > 0){
for(;\$thing<\$numthing;\$thing++){
\$result *= \$numthing - \$thing;
}
}
else{
\$result = 1;
}

echo \$numthing."! = ".\$result;
?>``````

## 8. By Jesus Eduardo CK π²π½

Made by Jesus Eduardo CK π²π½. PHP program made using recursion to get the factorial of a number. ( Source )

``````
<?php
function fact(\$num){
if(\$num == 1) {
return 1;
} else {
return \$num * fact(\$num - 1);
}
}

echo fact(5);

?>``````

## 9. By ubed khorajiya

Made by ubed khorajiya. ( Source )

``````<?php
\$f=1;
\$n=5;
for(\$k=1;\$k<=\$n;\$k++)
{
\$f=\$f*\$k;
}
echo \$f;
?>``````

## 10. By Dmitriy Laptiev

Made by Dmitriy Laptiev. ( Source )

``````<?php // by Dmitriy

function factorial(int \$index)
{
if (\$index === 1) return 1;
elseif(\$index<0) exit('invalid arg');
return \$index*factorial (\$index-1);
}

echo '3! = ', factorial (3), '<br/>'; ``````

## 11. By MIkhail Zhitkov

Made by MIkhail Zhitkov. Enter your number at the last line. ( Source )

``````<?php
function factorial(\$num){

\$result =1;

for (\$i = 1; \$i = \$num; \$i++){

\$result*= \$num;

\$num--;
}
return \$result;
}

echo factorial(7);

echo "<br />";

echo factorial(8);
?>``````

## 12. By Anton

Made by Anton. ( Source )

``````<?php

/*
* Get factorial in one line
*/

function f(\$n) {
return \$n === 1 ?: \$n * f(\$n - 1);
}

echo f(5);``````

## 13. By Akshay Harshora

Made by Akshay Harshora. Php factorial calculator to get the factorial of a positive integer. ( Source )

``````<?php
\$fact = 1;
for(\$i=1;\$i<=5;\$i++)
{
\$fact = \$fact * \$i;
}
\$i--;
echo "the factorial of number \$i is: \$fact";
?>``````

## 14. By Md. Shahajahan Ali

Made by Md. Shahajahan Ali. Factorial Number finder using Recursion in php. ( Source )

``````<?php

function facts(\$n)
{
if(\$n == 1)
return 1;

return \$n * facts(\$n - 1);

}
echo facts(5);

?>``````

## 15. By pretty collections

Made by pretty collections. ( Source )

``````<?php
\$i=5;
\$j=1;
for (\$k=1;\$k<=\$i;\$k++){
\$j=\$j*\$k;
}
echo "\$j";
?>``````

## 16. By Nirali Vachhani

Made by Nirali Vachhani. ( Source )

``````    <?php
\$num = 5;
\$factorial = 1;
for (\$x=\$num; \$x>=1; \$x--)
{
\$factorial = \$factorial * \$x;
echo \$factorial."=".\$factorial."*".\$x."<br/>";
}
echo "<br/><br/>Factorial of \$num is \$factorial";
?>  ``````

## 17. By Gaurav kumar

Made by Gaurav kumar. ( Source )

``````<?php
function fact(\$val)
{
\$factorialValule = 1;
for(\$i=1;\$i<=\$val;\$i++)
{
\$factorialValule  = \$factorialValule * \$i;
}
echo \$factorialValule;
}

echo fact(5);
?>
``````

## 18. By Amos Aidoo

Made by Amos Aidoo. Change the value of n to find factorial of that number. ( Source )

``````<?php
\$n = 5;
\$fact = 1;
for(\$x = \$n; \$x>1; \$x--){
\$fact*=\$x;
}
echo \$fact;
?>``````

## 19. By Jithin

Made by Jithin. ( Source )

``````<?php

function factorial(\$s){
\$f=1;
for(\$i=1;\$i<=\$s;\$i++){
\$f=\$f*\$i;
}
return \$f;
}
\$p = 10;
echo \$p."! = ".factorial (\$p);

?>``````

## 20. By Oleg Betinov

Made by Oleg Betinov. ( Source )

``````<?php
function Factorial(\$number){
if(\$number <= 1){
return 1;
}else return (\$number * Factorial (\$number - 1));
}
\$i = 1;
\$x = 10; //Max Factorial
while (\$i <= \$x) {
\$y = Factorial (\$i);
echo "\$i! = \$y <br />";
\$i++;
}
?>``````