The function is working 100% as intended. You do not print the value simply because the function is "returning" it. If you want the value printed, then you must echo it as you've observed yourself.
Think of it this way:
Return - returns the value in raw data.
echo - prints stuff.
Also the way your recursive function works now, it only contains the return value within its scope, so the value is "dropped" when the execution is completed. That's why you won't be able to echo anything. When you echo inside the function, it's echoing the value before stopping the execution.
One way to circumvent this, is by adding a print parameter to the function if you wish for your value to be printed,
Example:
function debug($a, $i, $print=false) {
$a .= $i;
if($i < 5) {
$i++;
debug('echo', $i, $print);
} else{
if($print){
echo $a;
}
return $a;
}
}
debug('echo', 0, true);