php recursion uses the example of php recursive function

  • 2021-01-03 20:51:44
  • OfStack


// Recursively retrieves the role ID string 
function explodeRole($roleObj, &$resultStr){
  if(0 < count($roleObj->childRoleObjArr)){
    foreach($roleObj->childRoleObjArr as $childRoleObj){
      if('' == $resultStr){
        $resultStr .= "{$childRoleObj->id}";
      }else{
        $resultStr .= ", {$childRoleObj->id}";
      }
      explodeRole($childRoleObj, $resultStr);
    }
  }
}

// Recursively gets an array of cascading role information 
function makeRoleRelation(&$roleObjArr){
  foreach($roleObjArr as $item){
    $item->childRoleObjArr = getRoleObjArrByParentId($item->id);
    if(0 < count($item->childRoleObjArr)){
      makeRoleRelation($item->childRoleObjArr);
    }
  }
}

// Through the parent role id Gets child role information   
function getRoleObjArrByParentId($parentid){
  $operCOGPSTRTSysRole = new COGPSTRTSysRole();
  $operCOGPSTRTSysRole->setColumn($operCOGPSTRTSysRole->getAllColumn());
  $operCOGPSTRTSysRole->setWhere("parentroleid={$parentid}");
  $roleObjArr = $operCOGPSTRTSysRole->convResult2ObjArr($operCOGPSTRTSysRole->selectTable());
  return isset($roleObjArr)?$roleObjArr:array();
}

The recursive function usage of php

A function that calls itself within its body is called a recursive call. Such functions are called recursive functions. This is often of great practical value to programmers, and is often used to break down complex problems into simple, identical cases, and do this repeatedly until the problem is solved.

The difference between using recursive functions and not using recursive functions

Example 1: Using static variables


function test(){
  static $dig=0;
  if($dig++<10){
    echo $dig;
    test();
  }
}
test();//12345678910

Example 2: Reverse string permutation using recursive functions and loops


function unreverse($str){
  for($i=1;$i<=strlen($str);$i++){
    echo substr($str,-$i,1);
  }
}
unreverse("abcdefg");//gfedcbc

function reverse($str){
  if(strlen($str)>0){
    reverse(substr($str,1));
    echo substr($str,0,1);
    return;
  }
}
reverse("abcdefg");//gfedcbc

Recursive functions are often replaced by loops, and I suggest using them when we can't, because loops are easier to understand and less error-prone.

php recursive functions php pays for recursive functions, which are calls to themselves, and these functions are particularly useful for browsing dynamic data structures, such as trees and lists.
Few web applications require complex data structures


<?php
function reversr_r($str)
{
if (strlen($str)>0)
reverse_r(substr($str,1));
echo substr($str,0,1);
return;
}
?>

<?php
function reverse_i($str)
{
for($i=1;$i<=strlen($str);$i++)
{
echo substr($str,-$i,1);
}
}

This program listing implements two functions, both of which print the contents of a string in reverse order
The function reversr_r is implemented recursively, while the function reverse_i() is implemented through a loop


Related articles: