Resolution of return value problem in PHP recursive function

Source: Internet
Author: User
In the previous article we introduced the use of PHP recursive functions, and the implementation of the method, then we use the PHP recursive function to return the problem? Today, we will give you an explanation of the return value in the PHP recursive function!

Return a value problem in a recursive function

/* Loop to remove the string to the left of the 0 */function Removeleftzero ($str) {if ($str [' 0 '] = = ' 0 ') {$str = substr ($str, ' 1 '); Removeleftzero ($STR);} Else{return $str;}}

In most people's view this code is not a problem, if not run a bit and do not know where the problem? If the recursion does not have a return value after this is run, there will be no return value after recursion, even if the else condition is met, and should be changed to

/* Loop to remove the string to the left of the 0 */function Removeleftzero ($str) {if ($str [' 0 '] = = ' 0 ') {$str = substr ($str, ' 1 '); return Removeleftzero ($str ); Add return value to function}else{return $str;}}

Summarize:

I believe that through this article, we have a new understanding of the return value problem in PHP recursive function, but also know how to solve it, and hope to help you!

Related recommendations:

Analysis of three ways to implement PHP recursive function


Example of using PHP recursive functions

How does php recursive function work? Typical examples of PHP recursive functions

What is PHP recursive function and simple example explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.