Home > Backend Development > PHP Tutorial > A brief analysis of the use of PHP recursive function return values

A brief analysis of the use of PHP recursive function return values

PHP中文网
Release: 2023-02-28 19:16:01
Original
1595 people have browsed it

After a long period of development of PHP, 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 count the visitors of his own website. . 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 PHP 1.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:

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

This code seems to be fine, but in fact there is a problem in the 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:

<?php 
    functiontest($i)  
    {  
    $i-=4;  
    if($i<3)  
    {  
    return$i;  
    }  
    else  
    {  
    returntest($i);//增加return,让函数返回值  
    }  
     
    }  
     
    echotest(30);  
 ?>
Copy after login

Related articles:
How to use PHP recursive function effectively? Typical examples of php recursive functions
php recursive function Introduction to Array related functions in PHP
php recursive function return may fail to return the desired value correctly

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template