Home > Backend Development > PHP Tutorial > 关于php递归函数的问题

关于php递归函数的问题

WBOY
Release: 2016-06-20 12:51:26
Original
1173 people have browsed it

<?phpfunction draw($total,$line=1,$result="a1"){    echo "<script>alert('$total,$line')</script>";	if($line>$total){        return;    }    else{        $line++;        //echo $result;        echo "<script>alert('draw1($total,$line,$result)')</script>";        draw($total,$line,$result);        echo "<script>alert('draw2($total,$line,$result)')</script>";	}	echo $result;}draw(3);?>
Copy after login

语句中执行函数draw(3),当运行到if($line>$total){return;}echo出来三条信息draw2(3,4,a1)-->draw2(3,3,a1)-->draw2(3,2,a1)然后结束,请问是什么 原因使$line在return发生后,依次减1呢?


回复讨论(解决方案)

没有原因,因为他并没有变

function draw($total,$line=1,$result="a1"){    echo "<script>alert('$total,$line')</script>";    if($line>$total){        return;    }    else{        $line++;        //echo $result;        echo "<script>alert('draw1($total,$line,$result)')</script>";echo "递归前 line = $line\n";        draw($total,$line,$result);echo "递归后 line = $line\n";        echo "<script>alert('draw2($total,$line,$result)')</script>";    }    echo $result;}draw(3);
Copy after login

谢谢,我想我递归的原理还没弄明白,再去找下资料吧

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template