Php recursive functions are valid only when used. php recursive functions are valid.

Source: Internet
Author: User

Php recursive functions are valid only when used. php recursive functions are valid.

For the usage of php recursive functions, I would like to share several examples of php recursive functions. In php programming, it is very common to use recursive functions for function calls. recursive functions are used well to improve code efficiency, use examples to learn how to use 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-> chil DRoleObjArr = 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 = $ operCOGPST RTSysRole-> 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");//gfedcbcfunction 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:

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.

Summary

The above section describes how to use php recursive functions. I hope it will help you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.