Home Backend Development PHP Tutorial PHP recursive function example analysis

PHP recursive function example analysis

Mar 06, 2018 am 11:38 AM
php function Case Analysis

This article

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

   public function digui($i)

    {

 

        if($i=='1'){

            echo  "*" ;

            echo "<br>";

            return ;

        }else{

            $this->digui($i-1);

            for($j=1;$j<=$i;$j++){

                echo  "*" ;

            }

            echo "<br>";

        }

    }

 调用 $this->digui(3);

结果

*

**

***

 

  function digui2($n){

        echo $n." ";

        if($n>0){

            $this-> digui2($n-1);

        }else{

            echo "<-->";

        }

        echo $n." ";

    }

Copy after login

Call $this->digui2(3);

Result

3 2 1 0 <-->0 1 2 3

Recursive function execution anatomy example (reposted from others)

Look at the following code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

&lt;?php

function one($num){

echo $num;

two($num-1);

echo $num;

}

function two($num){

echo $num;

three($num-1);

echo $num;

}

function three($num){

echo $num;

}

one(3);

?&gt;

Copy after login

The above code decomposes the test() function. We Thinking:
When executing the one(3) function, like the test() function, first output 3, and then call the two(2) function,
Note that the following 3 has not been output at this time,
Go on, execute the two(2) function, output 2, and call the three(1) function. Similarly, there is no time to output the following 2,
Execute three(1), output 1 directly, and no longer call other functions,
At this time, we wonder if the two() function just has not been executed yet. OK, then execute the unfinished part of the two() function. After the two() function is executed, the following 2 is output, and then starts Execute the unfinished part of the one() function, that is, output the following 3. At this time, all functions have been executed.
Then, the output result is:
3 2 1 2 3

Related recommendations:

Detailed explanation of php recursive function

Explanation on calling php recursive functions

Analysis of three ways to implement php recursive functions

The above is the detailed content of PHP recursive function example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

See all articles