Blogger Information
Blog 142
fans 5
comment 0
visits 130030
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
什么是递归函数?
php开发大牛
Original
1389 people have browsed it

一个函数在它的函数体内调用它自身称为递归调用。 这种函数称为递归函数。

php递归函数与非递归函数有什么区别?

例一:使用静态变量

代码示例:

function test(){
static $dig=0;
if($dig++<10){
 echo $dig;
 test();
}
}
test();//12345678910

例二:使用递归函数和循环实现字符串逆转排列

代码示例:

function unreverse($str){
for($i=1;$i<=strlen($str);$i++){
 echo substr($str,-$i,1);
}
}
unreverse("abcdefg");//gfedcbc
function reverse($str){
if(strlen($str)>0){
 reverse(substr($str,1));
 echo substr($str,0,1);
 return;
}
}
reverse("abcdefg");//gfedcbc


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post