I am trying to learn recursion and have written a function that takes a number and count it down to zero and then count it up to the original number again, the function works, but what I don't understand is why it works as it does. I understnad why the first print after the else statement prints: "5 4 3 2 1" and then the number is 0 and the if statement prints: "0". It is after this scenario I don't understand since now the function enters the second print after the else statement and prints: "1 2 3 4 5" which is very strange to me. I would really appreciate if someone could explain this to me.
<?php
function rec_downandup($num){
if($num == 0){
print '0 ';
}else{
print $num.' ';
rec_downandup($num-1);
print $num.' ';
}
}
rec_downandup(5);
?>
Output
5 4 3 2 1 0 1 2 3 4 5