Ngademin Pegiat Teknologi

Creating Recursive Functions With PHP

1 min read

Membuat Fungsi Rekursif Dengan PHP

Creating Recursive Functions With PHP – Hello, welcome back to the PHP step by step tutorial series from Malangding.com, this time I will discuss how to create a Recursive function in PHP. In this lesson we will learn what a recursive function is and how to create it using PHP.

First I want to explain what a function is. In the previous tutorial we have discussed what a function is and how to write a function using PHP. If you haven’t read it, please click here to see an introduction to PHP functions. A function is a program code that is made specifically to shorten an action in the program. In addition to the functions that have been provided directly by PHP, we can also create our own functions with the applicable rules. For example, the name cannot be the same as the function provided.

Creating Recursive Functions With PHP

Whereas recursive function is a function which the function calls itself. The process of the recursive function repeats itself because of the call itself, this recursive function will continue to run if there is no limit. So we need a delimiter to stop the recursive process from running. As an example I quoted from wikipedia, the recursive function can be likened to two mirrors in a parallel position between each other, the image captured is infinitely recursive, meaning that each mirror will reflect its own image.

Example of a recursive function using PHP

To understand more about recursive functions, here I want to explain an example that is implemented using PHP. In this example, for example, I will display the numbers 1 to 20. Here is the program code:

I created a function with the name array_number and the parameters in it are x and y. Where x is the variable that stores the number of repetitions of the number_series and Y is the variable that stores the number of times the iteration has been done, while 1 is the initial value of the iteration.

Next I need a constraint to limit the number of runs of the recursive function. The second line in the number_series function calls itself which means it will call itself as long as the total number of loops that have been done ($y) is less than the total number of loops (x). And the result looks like this:

Creating Recursive Functions With PHP

Creating Recursive Functions With PHP

Another example of another recursive application to factorial calculations. Fakorial is the result of multiplying positive integers that are less than or equal to n (wikipedia). Factorial is written with n! (factorial with number n). For example: factorial 3 (3!), then it means 3! = 3*2*1. When applied to a recursive function, it looks like this:

In the code snippet above I have a function with the name factorial and its parameters $n is the variable that holds the factorial. In the second line, it checks that if the factorial value is 1 then it will display the value of n itself. If the value is not other than one then the recursive function is executed, where the factorial value will be multiplied by the previous factorial value minus one. Then the result will be like this:

Recursive function with php

Recursive function with php

So the conclusions we can draw from the material Creating Recursive Functions With PHP are as follows:

  1. With the function, it will be very easy for us to develop the program
  2. The coolest thing about implementing recursive functions is that the applications we build are more efficient. For example, in making a thesis that applies the algorithm to the program using PHP. So by applying the recursive concept, it will make it easier to understand the program code and it doesn’t need to be repeated.

Okay, that’s all for the first time about Creating Recursive Functions With PHP, we will continue in the next material. If there is a shortage, we really hope that we can improve the quality of comments in the future.

Incoming search terms:

  • recursive if there are two functions
  • factorial number using php
  • how to mirror php
  • how to make a program using a recursive function by calculating the multiplication result with the addition method
  • how to use recursive function in php
  • sample image / php?checkout=
  • sample image / php?inlist=
  • Fibonacci tree example
  • example program c# poll numbers
  • make if with php function

Ngademin Pegiat Teknologi

Google menyediakan Earth Engine untuk semua bisnis dan pemerintah

Selama dekade terakhir, para peneliti di akademisi dan organisasi nirlaba telah memperoleh akses ke informasi yang semakin kompleks tentang permukaan bumi melalui Google Earth...
Ngademin
1 min read

Google menyediakan Earth Engine untuk semua bisnis dan pemerintah

Selama dekade terakhir, para peneliti di akademisi dan organisasi nirlaba telah memperoleh akses ke informasi yang semakin kompleks tentang permukaan bumi melalui Google Earth...
Ngademin
1 min read