A brief analysis of how to use the return value of PHP recursive function

  • 2020-05-30 19:30:25
  • OfStack


PHP was first created in 1994 by Rasmus Lerdorf as a simple program written in Perl language to count visitors to its own website. It was later rewritten in C, including the ability to access the database.


The first version was published in 1995 as Personal Home Page Tools (PHP Tools). Lerdorf wrote some documentation about the program and released PHP 1.0. In this early version, simple functions such as guest book, guest counter were provided. Since then, more and more websites have used PHP, and there is a strong demand for some features, such as loop statements and array variables, etc. After new members joined the development, PHP2.0 was released in the middle of 1995. The second edition was named PHP/FI(Form Interpreter). PHP/FI added support for mSQL, thus establishing PHP's position in dynamic web development. By the end of 1996, 15,000 websites were using PHP/FI; By the middle of 1997, the number of websites using PHP/FI exceeded 50,000. In the middle of 1997, the development plan for version 3 was started. The development team added Zeev Suraski and Andi Gutmans, and version 3 was named PHP3. In 2000, PHP 4.0 was introduced with many new features.

In my previous programming, I encountered a problem with an PHP recursive function, which is actually a very simple problem. The problem is the return value of the PHP recursive function. This is the beginning:


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

This code looks fine, but there is something wrong with else. test executed here does not return a value. So even though it's $i < At 3, the whole return$i function still doesn't return a value. The above PHP recursive function is modified as follows:


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


Related articles: