Skip to content

Instantly share code, notes, and snippets.

@UtmostCreator
Last active October 30, 2025 17: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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment