The recursion problem of the Bible cannot be solved 555 a day...

Source: Internet
Author: User
The recursion problem of the Bible cannot be solved 555 a day... This post was last edited by iamsai at 21:00:08

 0){   reverse_i(substr($str,1));                }echo substr($str,0,1);return;}?>


Please refer to my questions:
The output result of this program is the inverted olleh output.
After reverse_ I (substr ($ str, 1) in the loop body; hello ello llo lo o
Echo substr ($ str, 0, 1); output o.
What value does the return statement return? Is it returned to the fourth row? What is the subsequent execution order? Who can help me analyze? Thank you!


Reply to discussion (solution)

Return is the meaning of return, so the program of this function no longer runs below, and the function does not need to return values, so it is not added!

Can only output o?

Why does it output hello? Can someone help me analyze it?

You can add QQ directly,

643461761

$ Str = 'hello'; var_dump (reverse_ I ($ str); // check the function reverse_ I ($ str) {echo $ str, PHP_EOL; // Check if (strlen ($ str)> 0) {reverse_ I (substr ($ str, 1);} echo substr ($ str, 0, 1 ); return ;}
Hello
Ello
Llo
Lo
O

OllehNULL

$ Str = 'hello'; var_dump (reverse_ I ($ str); // check the function reverse_ I ($ str) {echo $ str, PHP_EOL; // Check if (strlen ($ str)> 0) {reverse_ I (substr ($ str, 1);} echo substr ($ str, 0, 1 ); return ;}
Hello
Ello
Llo
Lo
O

OllehNULL
Echo substr ($ str,); isn't the first character of the output string? After 0 is input, will return NULL and call the function program end? Why is llehNULL output?

If (strlen ($ str)> 0)
{
Reverse_ I (substr ($ str, 1); // This is a recursive call.
<--- Return here
}
Echo substr ($ str, 0, 1 );
Return;

After the first recursion, is Return the function body that returns the value of o to if? Why is it different from C? Isn't this value directly returned to the function that calls it?

After the first recursion, is Return the function body that returns the value of o to if? Why is it different from C? Isn't this value directly returned to the function that calls it?

After the first recursion, is Return the function body that returns the value of o to if? Why is it different from C? Isn't this value directly returned to the function that calls it?

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.