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

关于php递归函数的问题

WBOY
WBOYOriginal
2016-06-20 12:51:261165browse

<?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);?>

语句中执行函数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);

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

Statement:
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
Previous article:PHP 时间与日期Next article:PHP 静态分析器:phan