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

Source: Internet
Author: User
Tags php programming
For the use of PHP recursive functions, share several PHP Recursive FunctionsExample, in PHP programming, using recursive function calls is very common, recursive function is good, can improve code efficiency, through example learning the use of PHP recursive functions

First, what is a recursive function?

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

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

Example one: using static variables

code example:

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

Example two: Using recursive functions and loops to implement string reversal permutations

code example:

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

Second, PHP recursive function use instance

PHP recursion uses the example (PHP recursive function), which includes recursion to get the role ID string, recursively get the Cascade role information array, and get child role information through the ID of the parent role.

Example:

code example:

Recursively get the Role ID string function Exploderole ($ROLEOBJ, & $resultStr) {if (0 < count ($roleObj->childroleobjarr)) { foreach ($roleObj->childroleobjarr as $CHILDROLEOBJ) {if (' = = $resultStr) {$resultStr. = "{$CHILDROLEOBJ-&G      T;id} ";      }else{$resultStr. = ", {$childRoleObj->id}";    } exploderole ($CHILDROLEOBJ, $RESULTSTR); }}}//Recursive get Cascade role information Array function makerolerelation (& $ROLEOBJARR) {foreach ($roleObjArr as $item) {$item->childroleobj    ARR = Getroleobjarrbyparentid ($item->id);    if (0 < count ($item->childroleobjarr)) {makerolerelation ($item->childroleobjarr);  }}//Get child role information through the ID of the parent role 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: Use recursive functions and loops to implement string reversal permutations.

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 recycled, and it is recommended that you use loops instead, because we are easier to understand with loops and less prone to errors. PHP Recursive functions PHP pay recursive functions, recursive functions are called themselves, these functions are particularly useful for browsing dynamic data structures, such as trees and lists. Few Web applications require the use of complex data structures.

Example:

Copy Code code example:

0) Reverse_r (substr ($STR, 1)); Echo substr ($str, 0, 1); Return }?>

This manifest implements two functions, both of which can print the string in reverse order the content function Reversr_r is implemented recursively, and the function reverse_i () is implemented through loops.

The above is the PHP recursive function how to use only valid? PHP recursive function Typical examples of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

Related content recommended:

PHP Recursive function Introduction to the array correlation function in PHP

Analysis of the use of return value of PHP recursive function

PHP recursive function return occurs when the desired value is not returned correctly

PHP Custom Functions: recursive function Tutorial Video

  • Related Article

    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.