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

Mar 15, 2017 pm 03:44 PM
return value recursive function

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Three ways to get thread return value in Python Three ways to get thread return value in Python Apr 13, 2023 am 10:43 AM

When it comes to threads, your brain should have this impression: we can control when it starts, but we cannot control when it ends. So how do we get the return value of the thread? Today I will share some of my own practices. Method 1: Use a list of global variables to save the return value ret_values ​​= [] def thread_func(*args): ... value = ... ret_values.append(value) One reason to choose a list is: the append() of the list Methods are thread-safe, and in CPython, the GIL prevents concurrent access to them. If you use a custom data structure, and

How to solve the problem that scanf return value is ignored How to solve the problem that scanf return value is ignored Nov 14, 2023 am 10:01 AM

Solutions to the ignored return value of scanf include checking the return value of scanf, clearing the input buffer, and using fgets instead of scanf. Detailed introduction: 1. Check the return value of scanf. You should always check the return value of the scanf function. The return value of the scanf function is the number of successfully read parameters. If the return value is inconsistent with the expected one, it means that the input is incorrect; 2 , Clear the input buffer. When using the scanf function, if the input data does not match the expected format, the data in the input buffer will be lost, etc.

What are the optimization techniques for C++ recursive functions? What are the optimization techniques for C++ recursive functions? Apr 17, 2024 pm 12:24 PM

To optimize the performance of recursive functions, you can use the following techniques: Use tail recursion: Place recursive calls at the end of the function to avoid recursive overhead. Memoization: Store calculated results to avoid repeated calculations. Divide and conquer method: decompose the problem and solve the sub-problems recursively to improve efficiency.

How to solve the excessive function nesting error in Python code? How to solve the excessive function nesting error in Python code? Jun 25, 2023 pm 12:35 PM

Python is a very powerful programming language, and many programmers choose Python as their main programming language. However, too much function nesting in the code can make the program difficult to maintain and understand. This article will explore how to solve the excessive function nesting error in Python code. A brief introduction to function nesting Function nesting refers to the process of defining another function in the body of a function. Function nesting can make the structure of the program clearer and the code easier to read and maintain. However, too many nested functions can lead to an overly complex code structure.

Use Java's Math.min() function to compare the size of two numbers and return the smaller value Use Java's Math.min() function to compare the size of two numbers and return the smaller value Jul 25, 2023 pm 01:21 PM

Use Java's Math.min() function to compare the sizes of two numerical values ​​and return the smaller value. When developing Java applications, sometimes we need to compare the sizes of two numerical values ​​and return the smaller number. Java provides the Math.min() function to implement this function. The Math.min() function is a static method of the JavaMath class. It is used to compare the size of two values ​​​​and return the smaller number. Its syntax is as follows: publicstaticintmi

Application of C++ recursive functions in search algorithms? Application of C++ recursive functions in search algorithms? Apr 17, 2024 pm 04:30 PM

Recursive functions are used in search algorithms to explore tree-like data structures. Depth-first search uses a stack to explore nodes, while breadth-first search uses a queue to traverse layer by layer. In practical applications, such as finding files, recursive functions can be used to search for a given file in a specified directory.

What are the exit conditions for a recursive function in C++? What are the exit conditions for a recursive function in C++? Apr 17, 2024 am 11:33 AM

The exit conditions of C++ recursive functions include: Baseline conditions: Check whether the function reaches a state that can directly return results, usually judging whether a certain condition or parameter value meets the threshold. Recursion termination condition: Alternative to or in addition to the baseline condition, ensuring that the function stops after a certain number of recursive calls, by tracking the recursion depth or setting a maximum recursion depth limit.

Can a Golang function return multiple values? Can a Golang function return multiple values? Apr 13, 2024 pm 02:42 PM

Yes, Go functions can return multiple values ​​by returning a tuple, which is an immutable value that can contain different types of data.

See all articles