+ 10

Why echo() affects the output of this code so much?

I don't get the logic behind this piece of code đŸ€”. The original code outputs 17. Why? Trying to figure out what is going on using echos changes the final output to more predictable 1. Why? Also {} were omitted in the if-else section. Why?!! Please explain it to me 🙏

15th Feb 2021, 12:26 PM
Dual Core
Dual Core - avatar
3 RĂ©ponses
+ 9
Sorry 😐 I couldn't insert the code in the question. I tried. https://code.sololearn.com/wRG35NTF5CZO/?ref=app
15th Feb 2021, 12:31 PM
Dual Core
Dual Core - avatar
0
đ™›đ™žđ™§đ™šđ™© đ™„đ™–đ™§đ™© : I don't know about php but in some languages i have seen only first statement being executed without curly braces. I did a small google search and as i told you above is what is happening here. https://stackoverflow.com/questions/8726339/php-if-else-for-foreach-while-without-curly-braces/8726411#:~:text=12%20Answers&text=When%20you%20omit%20the%20braces,as%20body%20of%20the%20condition.&text=Internally%20it's%20the%20other%20way,a%20single%20%22grouped%22%20expression.&text=There%20are%20places%20where%20you%20can%2C%20but%20you%20never%20should. Print "taran=".$taran." a=".$a."<br>" After the global statement to get an idea of what is happening . But i will still try to explain you . The following function calls are taking place, num(5) num(4) num(3) num(2) num(1) num(0) num(1-3) // when code encounters value 0 , recursion for else statement stop and the call is returned back to num($a-3) with value of 1 . num(2-3) num(3-3)
15th Feb 2021, 1:32 PM
Abhay
Abhay - avatar
0
đ™šđ™šđ™˜đ™€đ™Łđ™™ đ™„đ™–đ™§đ™©: num(4-3) //again recursion(inner recursion) starts because 1 is greater than 0. num(1-1) num(1-3) num(5-3) num(2-1) num(1-1) num(1-3) num(2-3) Basically there are two types of recursion happening , one is inner and second is outer which makes it quite hard to understand what is going on, but if you really want to understand, take a copy and pen and start drawing the steps that are taking place .
15th Feb 2021, 1:49 PM
Abhay
Abhay - avatar