Factorial is a method of mathematics. If you have to create a program for factorial, then, first of all, you should know its mathematical logic. If you want to get a factorial of a number, first of all, decrement that number one by one and multiply all decremented numbers. The value comes after multiplication is called factorial. i.e. The factorial of 5 is -

5!=5*4*3*2*1=120

You have to use mathematics logic to find out the factorial of n number.The factorial of number ( 1,2,3,4,----n) is defined by the products of all number from 1 to n(Find factorial number ). As we discussed above the logic of mathematical to get factorial of any number. The factorial of 4 is -

4!=4*3*2*1=24

The factorial of 4 is 24 . Let's find out the factorial of another number. The factorial of 6 is -

6!=6*5*4*3*2*1=720

In PHP, we can create a script(program ) to find out the factorial of the number. Factorial means multiply one by one decrease numbers of anyone number.

Ex. Factorial of 9 is 9*8*7*6*5*4*3*2*1=362880

This is a simple mathematically method to find out the factorial of any number. Now, use this method in PHP and find out the factorial of any number.

```
<?Php
$no= 7;
$fact= 1;
$a=$no;
for($a=$no; $a>=1; $a--)
{
$fact= $fact* $a;
}
$msg="Factorial of $no is $fact";
?>
```
<?Php if(isset($msg))
{
echo $msg;
} ?>

Factorial of 7 is 5040

We can use the PHP for loop to find factorial of any number. In the above example, we create three variables. The first variable for number, second variable for factorial and in the third variable, we stored the number. Create a condition inside the for loop and decrement the third variable. In the for loop block, create logic for the product of 1 to n number. The for loop helps decrement number from n number to 1 and the block of code help in the product of these 1 to n number. This is the method of factorial program.

Create a program using the recursion method to find out the factorial of a number. In this example, we create an HTML form. In this HTML form, we create one text box and one button. Enter the number and click on the submit button to get a factorial of any number. In this program, we make a function. You can say the user-defined function. In the user-defined function, we pass a variable as an argument. Inside the function, the if condition helps for decision structure. If the number is 0, it returns 1. The Factorial of 0 is always 1. If the number is another, it returns factorial value.

```
<form action="" method="post">
Enter No. :
```

<?Php
if(isset($_POST['submit']))
{
$no=$_POST['no'];
function factorial($fact)
{
if($fact==0)
{
return 1;
}
return $fact*factorial($fact-1);
}
$msg=factorial($no);
}
?>
<?php if(isset($msg))
{
echo $msg;
} ?>

Run
In the above example, the HTML part is included.

The HTML form is used to input the number to find out the factorial.