recursion is calling one's self.
Code: Select all
<?php
function recurseTest( $val = 0 )
{
echo $val . "e;\n"e;;
recurseTest( $val + 1 );
}
recurseTest();
?>
on my machine it'll count from 0 to 870, where php stops the recursion on it's own.
Stack space is the active memory a function uses for each call. The more recursion you have, the more memory the application requires over it's life time, as illustrated using a function shamelessly stolen from php.net and the code above modified a bit:
Code: Select all
<?php
function getMemUsage()
{
if (function_exists('memory_get_usage'))
{
return memory_get_usage();
}
elseif ( strpos( strtolower($_SERVERї"e;OS"e;]), 'windows') !== false)
{
// Windows workaround
$output = array();
exec('tasklist /FI "e;PID eq ' . getmypid() . '"e; /FO LIST', $output);
return substr($outputї5], strpos($outputї5], ':') + 1);
}
else
{
return 'no value';
}
}
function recurseTest( $val = 0 )
{
echo $val . "e;\t"e; . getMemUsage() . "e;\n"e;;
recurseTest( $val + 1 );
}
recurseTest();
?>
Code: Select all
0 5,284 K
1 5,312 K
2 5,312 K
3 5,312 K
4 5,312 K
5 5,316 K
6 5,316 K
7 5,316 K
8 5,320 K
9 5,324 K
10 5,324 K
11 5,324 K
12 5,328 K
13 5,328 K
14 5,328 K
15 5,332 K
16 5,332 K
17 5,332 K
18 5,332 K
19 5,336 K
20 5,344 K
21 5,344 K
22 5,348 K
23 5,348 K
24 5,348 K
25 5,352 K
26 5,352 K
27 5,352 K
28 5,352 K
29 5,356 K
30 5,356 K
If each pass through a recursive function uses a bunch of memory, you could quickly run out of memory in php.