How does a php recursive function work? Typical examples of php recursive functions

Source: Internet
Author: User
How does a php recursive function work? Typical examples of php recursive functions: Usage of php recursive functions Php Recursive functionsFor example, in php programming, it is very common to use recursion for function calls. recursive functions are used well to improve code efficiency. Use of php recursive functions.

1. what is a recursive function?

A function is called recursively in its own body. This type of function is called a recursive function.

What is the difference between php recursive functions and non-recursive functions?

Example 1: Use static variables

Sample code:

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

Example 2: use recursive functions and loops to reverse string arrangement

Sample code:

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

II. Examples of php recursive functions

Example of php recursive use (php recursive function), including recursively obtaining the role ID string, recursively obtaining the cascade role information array, and obtaining the subrole information through the parent role id.

Example:

Sample code:

// Recursively obtain 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 retrieve the cascade role information array function makeRoleRelation (& $ roleObjArr) {foreach ($ roleObjArr as $ item) {$ item-> chi LdRoleObjArr = getRoleObjArrByParentId ($ item-> id); if (0 <count ($ item-> childRoleObjArr) {makeRoleRelation ($ item-> childRoleObjArr );}}} // Obtain the subrole information using the parent role id. function getRoleObjArrByParentId ($ parentid) {$ operCOGPSTRTSysRole = new COGPSTRTSysRole (); $ response-> setColumn ($ operCOGPSTRTSysRole-> getAllColumn (); $ operCOGPSTRTSysRole-> setWhere ("parentroleid = {$ parentid}"); $ roleObjArr = $ operCOGP STRTSysRole-> convResult2ObjArr ($ operCOGPSTRTSysRole-> selectTable (); return isset ($ roleObjArr )? $ RoleObjArr: array ();}

Php recursive function usage

Example 1: Use static variables to implement recursion.

Sample code:

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

Example 2: use recursive functions and loops to reverse the string arrangement.

Sample code:

 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 be replaced cyclically. we recommend that you use them when loop replacement is not available, because loops are easier to understand and error prone to errors. Php recursive functions: php payment recursive functions. recursive functions call themselves. these functions are especially suitable for browsing dynamic data structures, such as trees and lists. Almost no web applications require complex data structures.

Example:

Sample code:

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

In this program list, two functions are implemented. both functions can print the content of the string in reverse order. the reversr_r function is implemented through recursion, and the reverse_ I () function is implemented through loops.

The above is how php recursive functions are used to work? For details about typical examples of php recursive functions, refer to the PHP Chinese website (www.php1.cn )!

Recommended content:

Introduction to Array functions in php recursive functions

Analysis on the use of PHP recursive function return values

Php recursive function return may fail to return the expected value correctly.

Php user-defined function: Recursive function tutorial video

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.