# Factorial of a Number in PHP

Write a program to calculate factorial of a number in PHP. In this tutorial, We are going to write a PHP code to find factorial of a number using iterative as well as recursive approach. Before solving this problem let’s understand what is factorial.

Program to Print Factorial of a Number in PHP

If you are not familiar with the concept of recursive and iterative approach. Then check my previous tutorial on recursion Vs iteration.

## What is Factorial?

Factorial of a non-negative integer n (n!), is the product of all positive integers less than or equal to n.

For example – 5! = 5 * 4 * 3 * 2 * 1 = 120

Subscribe Our Tutorials

Fibonacci series program in PHP

Palindrome program in PHP

## Program to Calculate Factorial of a Number in PHP

Let’s write a function to calculate factorial of a number in PHP. In this code, we create a function calculateFactorial which takes a number as an argument and calculate it’s factorial using for loop.

## PHP Program to Find Factorial of a Number using Recursion

We have learned, how to calculate factorial of a number using iterative approach. Let’s write a PHP code to find factorial of a number using recursion.

Recursion concept with an example

Recursion vs iteration

Explanation

This is how this program is executed.

MCQ on recursion for practice

We can also do the same thing by creating a class.Let’s create a PHP class which calculate and print factorial of a number.