Example of using PHP recursive functions

Source: Internet
Author: User
PHP recursive function in our actual development is not a small part, for programmers, PHP recursive function roommate very high value, can solve a lot of problems, today we will introduce you to PHP recursive function use example!

This article mainly introduces the PHP recursive use example (PHP recursive function), including recursion to get the role ID string, recursively get the Cascade role information array, through the ID of the parent role to obtain child role information, the need for friends can refer to the next

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); }}//recursively gets the cascading role information array function makerolerelation (& $ROLEOBJARR) {foreach ($roleObjArr as $item) {$item->childroleobj    ARR = Getroleobjarrbyparentid ($item->id);    if (0 < count ($item->childroleobjarr)) {makerolerelation ($item->childroleobjarr);  }}//Gets the 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 ();} 

Recursive function Usage for PHP

A function calls itself in its function body called a recursive call. This function is called a recursive function. This is usually a high practical value for programmers, often used to break down complex problems into simple and identical situations, and do this repeatedly 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 permutations

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

Recursive functions many times we can recycle, suggesting that we don't use loops instead, because we're easier to understand with loops, and less prone to mistakes.

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 complex data structures to be used

<?phpfunction Reversr_r ($STR) {if (strlen ($STR) >0) Reverse_r (substr ($STR, 1)); Echo substr ($STR, 0,1); return;}? ><?phpfunction reverse_i ($STR) {for ($i =1; $i <=strlen ($STR); $i + +) {echo substr ($str,-$i, 1);}}

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

Summarize:

This article uses two examples to resolve the use of PHP recursive functions, the small partners can through this article on the PHP recursive function has a substantial understanding!

Related recommendations:

What is PHP recursive function and simple example explanation

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

Considerations for using return in PHP recursive functions you know what?

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.