PHP Recursive use example (PHP recursive function) _php instance

Source: Internet
Author: User
Tags data structures strlen
Recursive get Role ID string function Exploderole ($ROLEOBJ, & $resultStr) {if (0 < count ($roleObj->childroleobjarr)) {Fore Ach ($roleObj->childroleobjarr as $CHILDROLEOBJ) {if ("= = $resultStr) {$resultStr. =" {$CHILDROLEOBJ->
      ID} ";
      }else{$resultStr. = ", {$childRoleObj->id}";
    } exploderole ($CHILDROLEOBJ, $RESULTSTR); The Cascade role Information Array function makerolerelation (& $ROLEOBJARR) {foreach ($roleObjArr as $item) {$item->childr
    Oleobjarr = Getroleobjarrbyparentid ($item->id);
    if (0 < count ($item->childroleobjarr)) {makerolerelation ($item->childroleobjarr);
  The ID of the parent role gets the 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 ();

 }

Recursive function usage in PHP

A function calls itself in its function body called a recursive call. This function is called a recursive function. This is usually a very high practical value for programmers, and is often used to decompose complex problems into simple and identical situations, repeatedly doing this until the problem is solved.

The difference between recursive function and no recursive function

Example one: using static variables

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

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

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 we can iterate over many times, and suggest that we reuse them when we can't use loops instead, because we're easier to understand with loops, and less prone to error.

PHP recursive function PHP to pay recursive functions, recursive function is to call themselves, these functions are particularly suitable for browsing dynamic data structures, such as trees and lists.
Almost no web application requires complex data structures to be used

<?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 list of programs implements two functions, both of which can print the contents of the string in the opposite order
Function Reversr_r are implemented recursively, and function reverse_i () is implemented through loops

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.