Home »
PHP »
PHP Programs
PHP | Recursive program to print multiplication table of a number
Here, we are going to learn how to print the table of a given number using recursion in PHP?
By Nidhi Last updated : December 19, 2023
Prerequisites
To understand this example, you should have the basic knowledge of the following PHP topics:
Printing a table in PHP
Here, we will print the table of a specified number using the recursive call of a user-defined function. Function calling itself is known as a recursive function.
PHP program to print the table of a given number using recursion
The source code to print the table of a given number using recursion is given below. The given program is compiled and executed successfully.
<?php
//php program to print table of a given number
//using recursion.
function PrintTable($num, $temp)
{
if ($temp <= 10)
{
echo "$num X $temp = " . $num * $temp . "<br>";
PrintTable($num, $temp + 1);
}
}
PrintTable(5, 1);
?>
Output
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
Explanation
In the above program, we created a recursive function PrintTable(), here we made 10 recursive calls to print the table of a specified number with the help of if condition, the function gets finished when the value of $temp is greater than 10.
PHP Basic Programs »