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 = fact1 i.e. fact = 11 and it becomes face = 1;

For '2', the value of the fact variable will be fact = fact2 i.e. fact = 12 and it becomes fact = 2;

For '3', the value of the fact variable will be fact = fact3 i.e. fact = 23 and it becomes fact = 6;

For '4', the value of the fact variable will be fact = fact4 i.e. fact = 64 and it becomes fact = 24;

For '5', the value of the fact variable will be fact = fact5 i.e. fact = 245 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:-

<?php

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

    if(isset($_POST['save']))
    {

        if(!empty($_POST['fact_number']))
        {
            $factorial_number = $_POST['fact_number'];

            $fact = 1;
             for($i = 1; $i<=$factorial_number; $i++)
             {
                 $fact = $i*$fact; 

            }
        }
        else{
            $factorial_number_err = "Input is missing !";
            $fact = "";
        }
    }
 ?>
 <!DOCTYPE html>
<html lang="en">
<head>
  <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">
      .container{
          width: 40%;
      }
  </style>
</head>
<body>
<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">
    </div>
    <button type="submit" class="btn btn-primary" name="save">Submit</button>
  </form>
  <br>
  <p style="color: red;" class="fw-bold"><?php echo $factorial_number_err; ?></p>
    <?php
        if($fact!=""){
            echo '<p style="color: red;" class="fw-bold">The factorial of the given number is: '.$fact.'</p>';
        }
    ?>
</div>
</body>
</html>

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

Did you find this article valuable?

Support PBPHPSOLUTIONS by becoming a sponsor. Any amount is appreciated!