When and How to use Recursion Function in PHP

Recursion function in php with example:
After reading many blogs and forums about recursion function , and along with the expert advise in recursion function . Not everyone but most expert say not to use the recursion function until necessary . What I mean to say is do not use the recursion function , if the same problem can be solved without using the recursion function . But at the same time performance matters  a lot . When you are iterating through the unknown structures better use the recursion but be confident in using the recursion. I am not 100% sure whether it is the exact answer or not. But the reason behind not using the recursion function as far as you can is:

First the recursion function by itself is difficult to use.

Second debugging becomes quite logical or difficult.

The true fact I faced is , I used the recursion function but later on , it was difficult for me to debug it. Any way does not matter whether you use recursion function in your code or not but the thing is you have to know about the recursion function as well.

What is recursion function?

A recursive function is just a function that calls itself.

Things to take care of Using Recursive Function

  1. Every recursive function must have the terminating condition

2. The function must call the function itself and Each recursive call must be different than the one before

Lets go with the Common practical example of the recursive function for calculating the factorial:

We have using this example since we started programming.. .. Flash back …

Factorial Calculation without using Recursion:


//iterative factorial function

function factorial($number) {

$result = 1;

while ($number > 0) {

print "result = $result, number = $number\n";

$result *= $number;



return $result;



Lets do the same example using the recursion function :


//recursive factorial function

function factorial($number) {

if ($number < 2) {

return 1;

} else {

return ($number * factorial($number-1));



print factorial(6);


You did not find that much intresting … ok lets go with the real practical example:

First I will make the table category with three fields id, parent_id and category_name.

First column is the primary key and parent_id holds the parent key and last  is the name  of the category.

Id Parent_id Category_name
1 0 Jacket
2 0 Mufler
3 1 Wind proof
4 3 Bossini
5 2 Silk
6 1 Water proof
7 5 Kashmiri silk

Fig: table category

From the above table it is clear that there is hierarchy in the category name i.e.

  1. Jacket(id=1)
    1. Wind Proof(id=3)
      1. Bossini(id=4)
  2. Water Proof(id=6)

And similar hierarchy in other category . Why am I explaining all these long story is .. ..

My requirement:  Make the function that will accept the id or the category name as the parameter and return all its child or siblings.

i.e. when id 1 is passed as an parameter , it must return the 3 , 4 and 6 as the ouput. The output can be an array or just in the string format but must be 3, 4 and 6 according to the above table.

i.e when 3 is passed as an parameter the output must be 4

The solution seems to be simple …. Quick solution that has arise to your mind is ..

Select id from category where parent_id=1  ???????? is it the right solution

I don’t think so .. it will give only the first child . But solution is simple you can just make another similar function that returns the child and next and next function till the level of hierarchy if the level of hierarchy is not defined , then you will face the simple problem but its possible . Here where I want to focus is RECURSIVE FUNCTION.

Ok here is the recursion function that will help you to solve the above problem:

function get_all_sub_caseTypeId($parent_id){



$query = $this->CI->db->get('CASE_TYPE');

foreach($query->result() as $rows){

$child_ids[$rows->CASE_TYPE_ID] = ($rows->CASE_TYPE_ID);


$child_ids[$rows->CASE_TYPE_ID] = $this->get_all_sub_caseTypeId($rows->CASE_TYPE_ID);



return $child_ids;


function has_child($parent_id){



$query = $this->CI->db->get('category);


return false;


return true;


This example is made on the basis of CODEIGNITER

Conclusion  for appropriate Use of Recursive Function:

Everything that can be done iteratively can be done through recursively and vice versa. However, just because it is possible does not mean it is always appropriate or even easy to implement.

Recursion has a valuable niche in computer science. It provides a more natural path through unknown structures, a more intuitive way of writing many algorithms, and a more elegant approach to writing some kinds of code. Hopefully this article has helped to establish recursion as an important and useful tool in your retinue of PHP tactics. Happy coding!