Factorial Program in PHP with example

Factorial Program in PHP with example

Hi friends, in this tutorial, you will learn how to run the factorial program in PHP. I will explain the details in a step-by-step process. This is a very simple program and you will never forget it if you read this article once.

Also, read the OOPs concept in PHP with real-time examples

Steps to writing the factorial program in PHP

Step 1:- First of all, enter the input value in the HTML form.

Step 2:- Now, we will receive the input value with the help of $_POST superglobal variable for which the factorial will be calculated.

Step 3:- Next, we will declare some variables as shown below

$fact = "";
$factorial_number_err = "";

Step 4:- Now, we will check if the input value is not empty then we will run a for loop up to the number we have received from the HTML form.

Step 5:- Now, each of the incremented values of the for loop will be multiplied with the $fact variable in every iteration and store the result in the $fact variable as shown below

Suppose you have to find a factorial of 5, then the for loop will be executed up to 5.

  • For '1', the value of the fact variable will be fact = fact*1 i.e. fact = 1*1 and it becomes face = 1;
  • For '2', the value of the fact variable will be fact = fact*2 i.e. fact = 1*2 and it becomes fact = 2;
  • For '3', the value of the fact variable will be fact = fact*3 i.e. fact = 2*3 and it becomes fact = 6;
  • For '4', the value of the fact variable will be fact = fact*4 i.e. fact = 6*4 and it becomes fact = 24;
  • For '5', the value of the fact variable will be fact = fact*5 i.e. fact = 24*5 and it becomes fact = 120

Step 6:- From the above step, you can see that the final factorial value of the entered number is 120.

Complete Code:-


	$factorial_number_err = "";
	$fact = "";


			$factorial_number = $_POST['fact_number'];

			$fact = 1;
		 	for($i = 1; $i<=$factorial_number; $i++)
		 		$fact = $i*$fact; 
			$factorial_number_err = "Input is missing !";
			$fact = "";
 <!DOCTYPE html>
<html lang="en">
  <title>Factorial Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.1/dist/js/bootstrap.bundle.min.js"></script>
  <style type="text/css">
  		width: 40%;
<div class="container mt-3">
  <h2>Factorial program in PHP</h2>
  <form action="" method="post">
    <div class="mb-3 mt-3">
      <label for="email" class="fw-bold">Please enter the number:</label>
      <input type="number" class="form-control" id="email" placeholder="Enter The Number" name="fact_number">
    <button type="submit" class="btn btn-primary" name="save">Submit</button>
  <p style="color: red;" class="fw-bold"><?php echo $factorial_number_err; ?></p>
    		echo '<p style="color: red;" class="fw-bold">The factorial of the given number is: '.$fact.'</p>';

Conclusion:- I hope this tutorial will help you to understand the concept. If there is any doubt then please leave a comment below.

Leave a Comment