Considerations for using return in PHP recursive functions

Source: Internet
Author: User

PHP recursive function in the use of return when you encounter can not be properly returned to the desired situation, let me give an example to illustrate the

PHP recursive function in the use of return when you can not correctly returned to the desired situation, if you do not understand the reasons, it is difficult to find the wrong, the following specific examples to illustrate it: The code is as follows: function test ($i) {$i-=4;  if ($i <3) {return $i;  }else{test ($i);    } echotest (30); This code does not seem to be a problem, if you do not run an estimate you will not think he has any problems, in time to run up to find that there are problems you do not necessarily know where there is a problem, but in fact this function else inside is problematic. The result of executing in this code is no return value. So while satisfying the condition $i <3 return $i The entire function still does not returns a value.  Therefore, the above PHP recursive function can be modified as follows (more PHP tutorials please visit the Code Home): The code is as follows: function test ($i) {$i-=4;  if ($i <3) {return $i; }else{return test ($i);//Add returns, let function back value} echotest (30);
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.