Skip to content

Instantly share code, notes, and snippets.

@UtmostCreator
Last active December 11, 2025 23:03
Show Gist options
  • Select an option

  • Save UtmostCreator/1dd490493fd0137d40ab0c97db17f83f to your computer and use it in GitHub Desktop.

Select an option

Save UtmostCreator/1dd490493fd0137d40ab0c97db17f83f to your computer and use it in GitHub Desktop.
recursion function and its own stack frame
<?php
function rDemo($n, $level = 1): void {
$indent = str_repeat(" ", $level - 1);
echo $indent . ' => Entering level ' . $level . "(n={$n})" . PHP_EOL;
if ($n <= 0) {
echo $indent . "Base case reached at level " . $level . " (n = " . $n . ")" . PHP_EOL;
return;
}
rDemo($n - 1, $level + 1);
echo $indent . ' <= Returning from level ' . $level . "(n={$n})" . PHP_EOL;
}
rDemo(3);
@UtmostCreator
Copy link
Author

Output:

=> Entering level 1(n=3)
     => Entering level 2(n=2)
         => Entering level 3(n=1)
             => Entering level 4(n=0)
            Base case reached at level 4 (n = 0)
         <= Returning from level 3(n=1)
     <= Returning from level 2(n=2)
 <= Returning from level 1(n=3)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment