Home >>PHP Programs >PHP Program to print the factorial of a given number
In this example, we will create a PHP program to print the factorial of a given number. The factorial of a number n is defined by the product of all the digits from 1 to n.
<?php
$num = 7;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
?>
<?php
extract($_REQUEST);
if(isset($check))
{
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form method="post">
<table>
<tr>
<td>Enter Your Number</td>
<td><input type="text" name="num" required/></td>
</tr>
<td colspan="2" align="center">
<input type="submit" value="Print Factorial" name="check"/>
</tr>
</form>
</body>
</html>
</html>