How to use the recursive function of PHP

Source: Internet
Author: User
Tags php server php programming
This time to bring you the PHP recursive function how to use, the use of PHP recursive function of the considerations are what, the following is the actual case, together to see.

For the use of PHP recursive functions, share several examples of PHP recursive functions, in PHP programming, using recursive function call is very common, recursive function is good, can improve the efficiency of the code, through the example to learn the use of PHP recursive function.

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->id}";  }else{$resultStr. = ", {$childRoleObj->id}"; } exploderole ($CHILDROLEOBJ, $RESULTSTR); }}//recursive get Cascade role information Array function makerolerelation (& $ROLEOBJARR) {foreach ($roleObjArr as $item) {$item Childroleobjarr = 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");//gfedcbcfunction 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:

code example:

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.

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

PHP Custom two-dimensional array sorting function array

PHP server-side API and interface development detailed

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.