Factorial Program in PHP

Write a program to print factorial of a number in PHP. In this tutorial, I’ll explain how to write a factorial program in PHP using iterative as well as recursive approach. Before solving this problem let’s understand 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

Fibonacci series program in PHP.

Palindrome program in PHP.

Factorial Program in PHP using Iteration

Let’s write a program to calculate factorial of a number using an iterative approach.

Subscribe Our Tutorials

Get Latest Updates on Facebook

Factorial Program in PHP using Recursion

We have learned, how to calculate factorial of a number using iteration. Let’s write a program using recursion.

Recursion concept with an example.

Recursion vs iteration


This is how this program is executed.

MCQ on recursion for practice.

In the last example, let’s calculate factorial of a number by creating a class.

About WebRewrite

I am technology lover who loves to keep updated with latest technology. My interest field is Web Development.
Tagged , , . Bookmark the permalink.