php递归问题
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --> function test($a) { echo ' '.$a.' '; if($a >0) { test($a-1); }else { echo ''; } echo '<b>'.$a.'</b>'; } test(3)
3 2 1 0 0 1 2 3
各位大侠给解释下运行步骤,或者点参考资料让我去研究下,本人菜鸟。多多包涵
------解决方案--------------------
兄弟,一言难尽了,调用一函数,就得开辟一新栈,递归调用test($a-1)完时,已经输出了3 2 1 0 这个明白不?
然后接着当然是这玩意了,是吧, 明白吗?
最后回收开辟的栈,都要执行这玩意echo ''.$a.''; ~~~
哪里看不明白,指出,尽我所能给你仔细讲解哈~~
------解决方案--------------------
每一层都一样:
1,打印$a
2,进入下一层递归 or 打印
3, 打印$a
那么就是:
打印3, 打印2, 打印1, 打印0, 打印,打印0, 打印1, 打印2, 打印3。
红色是步骤1,蓝色是步骤2.
------解决方案--------------------
test(3) 执行function test :
1. echo $a = 3 然后进入if判断 3>0 执行 test(2) ;这里函数test(3) 还没有执行完的。是先去执行了 test(2) ------- *1;
2. test(2) echo $a = 2 然后判断2>0 执行 test(1) ; 这里也是test(2) 还没有执行完。先去执行了 test(1) ------------*2;
3.test(1) echo $a = 1; 然后判断1>0 执行 test(0) ;这里test(1) 还没有执行完。先去执行了test(0) -----------*3;
4. test(0) echo $a = 0 ;然后判断 0>0 执行了echo echo 0;
5. 4执行完后回到了 *3 ,判断完了后执行 echo $a = 1;
6. 5执行完 回到 * 2 echo $a = 2;
7. 6执行完 回到 *1 echo $a = 3;
这是 一层一层的深入 然后又是一层一层的往外走。

LeicareleasedtheLeicaLuxcameraappfortheAppleiPhoneafewdaysago.However,theappwasnotdevelopedbyLeica,butbyFjorden.ThecompanyhasbeenknownprimarilyforitscameragripsfortheiPhoneandwasacquiredbyLeicainDecember2023.Fo

SincethedemiseofLogitech'spopularHarmonyremotecontrols,themarketforhigh-qualityuniversalremotecontrolshasbeenfragmentedatbest.UnfoldedCircleaimstoavoidthefateoftheHarmonyUltimatebyeliminatinganyserverobligationsorsubs

Anintriguingthird-partycasefortheGalaxyS24Ultra(curr.$1,099.99onAmazon)hasappearedonmarketplaceslikeAliExpress.Astheimagesthroughoutthisarticleshow,thecasehasasimplesiliconeconstruction.However,italsocontainsanEInkd

WhiletheLightPhone2from2018wasstillequippedwithaneconomicale-inkdisplay,theLightPhone3usesanOLEDdisplaythatcanonlydisplaygrayscale.Thereasonfortheswitchtothe3.92-inchOLEDpanelwithitsresolutionof1,240x1,080isth

In2012,theeminentautomotivejournalistJeremyClarksonstatedthattheLamborghiniAventador,whichhewasreviewingatthetime,wouldbeamongthelastcarstofeatureanaturallyaspiratedV12.Morethanadecadelater,V12-poweredsupercarsareal

Audio-TechnicahasunveiledtheATH-S300BTwirelessheadphoneswithhear-throughnoise-cancellation,multipointpairing,and90hoursofbatterylife.Thenoise-cancellingfeaturehasthreemodes:off,on,andhear-through,whereambientsoundscanbehea

TheJabraEliteserieshascometoanend,withGNCEOPeterKarlstromerannouncingthediscontinuationofthewirelessearbudswithinthelineupinapressrelease.Petersaysthatthechangingmarketdynamicsandhowchallengingithasbecometogetas

Backin2022,whenPCIe7.0wasstartingtotakeshapeasafuturestandard,PCIe5.0wasjusthittingtheservermarketsandtheprospectofseeingPCIe6.0devicesavailableanytimesoon,letalonePCIe7.0ones,wasquitefar-fetched.CurrentlyPCIe5


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
