PHP Recursive Function

PHP Recursive Function. The PHP programming language presents the use of different capabilities that allow us to develop easy and complex applications. The capabilities have been performed in the program using PHP keywords that are written in the statement to capture the requirement. The functionalities support the application development which is helped by the logic. In this chapter, we are going to learn about PHP Recursive Function.

Recursion may be inspected as an offer that lets us call the function by the statement written within it. Recursion is the capability that is maintained by languages like C/ C++. We will be performing recursion in PHP using the function. Before we land into the depth of recursion now keep in mind that what is the actual definition of recursion is what it means in programming terms as well. Below we are learning about PHP recursive function with an example:

Program to Print Number | PHP Recursive Function

To surmise the idea of recursion, let us analyze the example. Now, we will be using the way to print the number, but the only method it will be distinct from the other program is by the use of recursion in this. We will be the function of the statement described within the same function.

To present the capability of recursion, we will be placing the login in the way so that it calls the function over and over till a special condition becomes filled. In standard cases where we need to implement the recursion, we just do that by using the loop but when it comes to performing the concept of looping without the loop, we can perform the same capability using the recursion.

The example that we are going to use in printing the numbers will be very useful to use to perform recursion without using the loop statement. The program will first specify the function that will be used to implement the recursion tool. The program will be possessing the function within it with the same name and that function will be named by using the function enclosed within it.

Although the following program seems simple, it will be very effective to boost your intelligence of PHP recursive function. Here is the code of the program that will be used to print the numbers from one to four.

function show_number($num) {
echo "Number is $num <br/>";
Number is 1
Number is 2
Number is 3
Number is 4

This program will be printing the number from one to four and the string “Number is” will be there before the number is written. In this program, the function that is used to print the number is the name show_number and $num is the name of the variable that will help the show_number function to get some value that will eventually lead to invoking it.

The IF statement is used to complete the condition checking. The program will continue on executing until the decided value is stored in the $num PHP variable is less than five. Once the value saved in it passes the value of four, the condition that must be satisfied to run the program more will go false and the program will be stopped. Here is the output of the above program.


The function recursion is studied as something very valuable when there is any requirement in the code to bring the recursion functionality without using the loops. Although we have done one of the simple program that use recursion to calculate the factorial and to print the numbers, there are ways besides several features that can be introduced in the application using this function recursion.

Scroll to Top