+ 3

Php function

I have this code from one of the php questions in sololearn quiz: <?php function math($t){ if($t==0) return 0; return $t+ math($t-1); } echo math(6); ?> The output is 21. Can somebody explain me step by step the result. I tried but i quit. :) Thank you

11th Aug 2018, 10:55 PM
razvan
razvan - avatar
5 Respuestas
+ 3
i think it adds up all numbers 0 to n and outputs it 6+5+4+3+2+1=21 it uses recursion btw
11th Aug 2018, 11:04 PM
GoodOldTimes
GoodOldTimes - avatar
+ 5
thank you
11th Aug 2018, 11:15 PM
razvan
razvan - avatar
+ 3
yes..but this part i don t get: return $t + math($t-1)
11th Aug 2018, 11:05 PM
razvan
razvan - avatar
+ 3
if you noticed the name of function is math and math is used again in itself so everytime it runs local variable t gets 1 lower
11th Aug 2018, 11:07 PM
GoodOldTimes
GoodOldTimes - avatar
+ 2
razvan youre welcome ^-^
11th Aug 2018, 11:17 PM
GoodOldTimes
GoodOldTimes - avatar