Home  >  Article  >  Backend Development  >  A brief analysis of how to use return values ​​of PHP recursive functions_PHP Tutorial

A brief analysis of how to use return values ​​of PHP recursive functions_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:12:52821browse


After a long period of development, many users are familiar with PHP. PHP was originally created by Rasmus Lerdorf in 1994. At the beginning, it was just a simple program written in Perl language to collect statistics on his own website. of visitors. Later it was rewritten in C language, including access to the database.


In 1995, the first version was released as Personal Home Page Tools (PHP Tools). Lerdorf wrote some documents introducing this program and released PHP1.0. In this early version, simple functions such as guest book and visitor counter were provided. In the future, more and more websites used PHP, and there were strong requests to add some features, such as loop statements and array variables, etc. After new members joined the development team, PHP 2.0 was released in mid-1995. The second version is named PHP/FI (Form Interpreter). PHP/FI added support for mSQL, and has since established PHP's position in dynamic web development. By the end of 1996, there were 15,000 websites using PHP/FI; by mid-1997, the number of websites using PHP/FI exceeded 50,000. In mid-1997, the development plan for the third version was started. The development team joined Zeev Suraski and Andi Gutmans, and the third version was named PHP3. In 2000, PHP4.0 came out again, which added many new features. characteristic.

I encountered a PHP recursive function problem in my previous programming. It was actually a very simple problem. The problem is with the return value of the PHP recursive function. This is what I started writing:

Copy code The code is as follows:

functiontest($i)
{
$i-=4; if($i<3)
{
return$i;
}
else
{
test($i);
}
}
echotest(30);
?>

This code seems to be fine, but in fact there is a problem in else. The test executed here has no return value. Therefore, although the condition $i<3 is met, the entire function return$i will not return a value. Make the following modifications to the above PHP recursive function:

Copy code The code is as follows:

functiontest($i)
{
$i-=4; if($i<3)
{
return$i;
}
else
{
returntest($i);//Increase return , let the function return the value
}
}
echotest(30);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326604.htmlTechArticleAfter a long period of development, many users are familiar with PHP. PHP was originally created by Rasmus Lerdorf in 1994 , it just started as a simple program written in Perl language to count...
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