浅谈PHP第四弹---递归函数
很多同学在学习递归函数的时候会感到头晕,无法搞清楚递归函数的原理和运行机制,本文将给大家详细讲解递归函数的运行机制和运用。
那什么是递归函数呢?
递归函数即为自调用函数,在函数体内直接或间接自己调用自己,但需要设置自调用的条件,若满足条件,则调用函数本身,若不满足则终止本函数的自调用,然后把目前流程的主控权交回给上一层函数来执行,可能这样给大家讲解,还是很难明白。
好,那下面我们就拿高洛峰老师的《细说PHP》中的例子来给大家讲解。
function test($n){
echo $n." ";
if($n>0){
test($n-1);
}else{
echo "";
}
echo $n." ";
}
test(10);
?>
大家首先思考一下,这个例子最终的输出结果是什么?
好,我们来看一下本函数输出的结果:
10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10
怎么样,不知道这个结果是否跟大家设想的一样呢?
好,下面我一步一步来给大家讲解...
第一步,执行test(10),echo 10,然后因为10>0,执行test(9),后面还有没来得及执行的echo 10
第二步,执行test(9),echo 9,然后因为9>0,执行test(8),同样后面还有没来得及执行的 echo 9
第三步,执行test(8),echo 8,然后因为8>0,执行test(7),同样后面还有没来得及执行的 echo 8
第四步,执行test(7),echo 7,然后因为7>0,执行test(6),同样后面还有没来得及执行的 echo 7
第五步,执行test(6),echo 6,然后因为6>0,执行test(5),同样后面还有没来得及执行的 echo 6
...........
第十步,执行test(0),echo 0,此时0>0的条件不满足,不在执行test()函数,而是echo “”,并且执行后面的 echo 0
10 9 8 7 6 5 4 3 2 1 0 0 1 2 3 4 5 6 7 8 9 10
此时,输出的内容如上述显示的红色部分,此时函数已经不再调用自己,开始将流程的主控权交回给上一层函数来执行
也就是开始执行刚刚所有test()函数没来得及输出的最后一个echo
它的流程是这样子的:
在函数执行的第一到第十步,函数输出的的是绿色部分,红色部分还“没来及”输出,就该调用自己执行操作,依次类推,直到流程执行到不再满足调用自己的条件,输出“”,此时,流程该执行前面“没来及”输出的代码。
就像我们平时玩的游戏一样,打死一个怪物,掉出一个宝贝,但是还有其他怪物在等着你来消灭,你不得不消灭完所有的怪物才能回来一个一个地拾宝贝。
怎么样,这么样跟大家来讲解是不是明白了呢?
有的同学又会问了,我在执行完所有的test函数之后,最终输出0
也就是输出到这里,
10 9 8 7 6 5 4 3 2 1 0 0
那为什么下一个输出的是 1 ,而不是 10 呢,
对于这个问题,为了帮助大家理解,下面我再给大家举一个例子:
看如下代码:
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);
?>
以上代码对test()函数进行分解操作,我们思考:
执行one(3)函数的时候,同test()函数一样,首先要输出3,然后调用two(2)函数,
注意,此时还没有输出下面的3,
接着走,执行two(2)函数,输出2,调用three(1)函数,同样,这里没有来得及输出下面的2,
执行three(1),直接输出1,不在调用其它函数,
此时,我们想刚刚的two()函数是不是还没有执行完,好,接着执行two()函数没有完成的部分,two()函数执行完之后,也就是输出下面的2,然后开始执行one()函数没有执行完的部分,也就是输出下面的3,此时所有函数执行完毕。
那么,输出结果是:
3 2 1 2 3
怎么样,大家这样理解起来是不是会容易些呢?
如果还是不太明白,请在下边跟帖提问,也请给出宝贵建议,我将会做出相应改进和讲解。
摘自 zdrjlamp

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
