EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Recursive Function

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Recursive Function

PHP Recursive Function

Introduction to PHP Recursive Function

The programming languages provide the use of several functionalities that enable us to develop simple and complicated applications. The functionalities have been implemented in the program using keywords that are written in the statement to satisfy the requirement. The functionalities endorse the application development which is facilitated by the logic. In this article, we are going to learn about PHP Recursive Function. Recursion may be considered as an approach that lets us call the function by the statement written within it. Recursion is the functionality that is supported by languages like C/C++. We will be implementing recursion in PHP using the function. Before we get into the depth of recursion just keep in mind that what is the actual meaning of recursion is what it means in programming terms as well. Below we are learning about PHP recursive function examples:

Examples of PHP Recursive Function

Below are the examples of PHP recursive function:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. Program to Print Number

To understand the concept of recursion, let us consider some examples. In this example, we will be using the method to print the number, but the only way it will be different from the other program is by the use of recursion in this. We will be calling the function from the statement defined within the same function. To provide the functionality of recursion, we will be putting the login in the way so that it calls the function over and over till a particular condition gets satisfied. In normal cases where we need to implement the recursion, we simply do that by using the loop but when it comes to implementing the concept of looping without the loop, we can achieve the same functionality 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 define the function that will be used to implement the recursion mechanism. The program will be having the function within it with the same name and that function will be called by using the function defined within it. Though the below program looks simple, it will be very helpful to fortify your understanding of recursive functions. Below is the code of the program that will be used to print the numbers.

Code:

<?php
function show_number($digit) {
if($digit<8){
echo "The number is $digit <br/>";
show_number($digit+1);
}
}
show_number(1);
?>

This program will be printing the number from one to seven and the string “The number is” will be there before the number is printed. In this program, the function that is used to print the number is name show_number and digit is the name of the variable that will help the show_number function to get some value that will eventually lead to invoke it. The IF statement is used to perform the condition checking. The program will keep on executing until the fixed value is stored in the digit variable is less than eight. Once the value stored in it exceeds the value of seven, the condition that must be satisfied to execute the program further will go false and the program will be terminated. Below is the output of this program.

Output:

PHP Recursive Function Example 1

2. Program to Find Factorial Number by Recursive Function

In the last program, we learned how to leverage recursion to print the number. Now in this program, we will learn how to change the logic of the application to find the factorial. Before we begin to write code for calculating factorial, it is important to understand what is factorial. Factorial of any number is the value which is obtained by reducing the number by one and then multiplying the outcome by the number and it has to be repeated till one. For example, if we need to calculate the factorial of 4 that it can be calculated using the expression 4*3*2*1. So the outcome will be 24. In the below program, the value will be given in the program. The program will process the value to calculate the outcome of the factorial. The value will be passed through the function and then all the logic written will be imposed on it to calculate the outcome. Below is the program so let’s proceed to have a look at it.

Code:

<?php
function calculate_fact($val)
{
if ($val === 0)
{
return 1;
}
else
{
return $val * calculate_fact($val-1);
}
}
echo "The factorial is of the given number is". calculate_fact(4);
?>

Output:

PHP Recursive Function Example 2

The above-written code is the implementation of the factorial using PHP. The name of the function is calculate_fact that will be used to calculate the factorial. The function with the same name has been called within it that is used to implement the mechanism of factorial in the program. Val is the variable that will be storing the value of which we have to find the factorial. We have used the IF condition checking to make sure that it is meeting the requirements that are considered essential when it comes to calculating the factorial of any value. In the very last line the main call of the calculate_fact function has been done that has invoked the functionality defined in this function. At this time we have passed four as we wanted to calculate the factorial of four. In case if you want to try this code with different values and replace the digit 4 in this program with the value of which you want to find the factorial.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,726 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Conclusion

The function recursion is considered something very useful when there is any need in the program to bring the recursion functionality without using the loops. Though we have used two of the simple programs that use recursion to calculate the factorial and to print the numbers, there are way too many features that can be introduced in the application using this function recursion.

Recommended Article

This is a guide to PHP Recursive Function. Here we discuss the introduction to PHP Recursive Function examples along with code implementation and output. You can also go through our other suggested articles to learn more –

  1. Palindrome in PHP (Examples)
  2. What is Abstract Class in PHP?
  3. Socket Programming in PHP with Methods
  4. Introduction to Factorial in PHP
  5. Factorial Program in JavaScript
  6. Palindrome in C++
  7. Palindrome in JavaScript
  8. Recursive Function in C
  9. Recursive Function in JavaScript
  10. Socket Programming in Python

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • PHP strpos
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More