Q:

Write a php program to print numbers from 10 to 1 using the recursion function

belongs to collection: PHP Programming Exercises

0

Recursion Function in PHP

Write a php program to print numbers from 10 to 1 using the recursion function.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

Recursion function is a function which is call itself. In the given example, we have called the recursion function to print the prime numbers from 10 to 1. A recursion function continues until some condition is met to prevent it. That's why, we use the if statement to break the infinite recursion.

<?php  
function countdown($num_arg) 
{
	if($num_arg > 0)
	{
		print("Number is $num_arg<br>");
		countdown($num_arg - 1);
	}
}
countdown(10);
?>

Output of the above code

Number is 10
Number is 9
Number is 8
Number is 7
Number is 6
Number is 5
Number is 4
Number is 3
Number is 2
Number is 1

In the recursion function, it is important to maintain the base value. If the base value is never invoked, in the above case, the infinite recursion function happened or it goes down to negative numbers. In the above example, countdown() function calls itself.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP Programming Exercises

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a php program to store the username in cooki... >>
<< Write a program in PHP to print prime numbers betw...