How to use php recursive function to be effective

  • 2021-09-12 00:35:56
  • OfStack

About the usage of php recursive function, share several examples of php recursive function. In php programming, it is very common to use recursive function call. Recursive function is well used, which can improve code efficiency. Learn the use of php recursive function through examples.

1. What is a recursive function?

A function calls itself in its body, which is called recursive call. This function is called a recursive function.

php What is the difference between a recursive function and a non-recursive function?

Example 1: Using static variables

Code example:


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

Example 2: Using recursive function and loop to realize reverse arrangement of strings

Code example:


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

2. php recursive function using instances

Examples of php recursive use (php recursive function) include recursively obtaining role ID string, recursively obtaining cascading role information array, and obtaining child role information through id of parent role.

Examples:

Code example:


// Recursive Acquisition of Roles 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 fetching cascading role information arrays 
function makeRoleRelation(&$roleObjArr){
 foreach($roleObjArr as $item){
 $item->childRoleObjArr = getRoleObjArrByParentId($item->id);
 if(0 < count($item->childRoleObjArr)){
  makeRoleRelation($item->childRoleObjArr);
 }
 }
}
// Through the parent role's id Getting subrole 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();
}

php Recursive Function Usage

Example 1: Using static variables to implement recursion.

Code example:


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

Example 2: Using recursive functions and loops to reverse the arrangement of strings.

Code example:


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

php recursive functions can sometimes be replaced by loops. It is recommended to use them when they cannot be replaced by loops, because loops are easier for us to understand and less prone to errors. php Recursive Function php pays for recursive functions, which are called themselves, and these functions are especially suitable for browsing dynamic data structures, such as trees and lists. Few web applications require complex data structures.

Examples:

Code example:


reverse_r(substr($str,1)); echo substr($str,0,1); return; } ?>

This listing implements two functions, both of which print the contents of strings in reverse order. The function reversr_r is implemented recursively, while the function reverse_i () is implemented by loop.

Summarize


Related articles: