Scope of a Variable in Recursive Function
Scope of a Variable in Recursive Function
A recursive function is one that calls itself, often over and over again. These are sometimes used to build tree views when data is hierarchical or for calculating some values using some procedure or logic. When calling the same function over and over again, we’re reusing the same variable names. But Its does as follows.
<?php
function recursive($count)
{
if($count==0)
exit;
if($count % 2==0)
{
echo “Even :”.$count . “<br>”;
recursive(–$count);
}
else
{
echo “Odd :”.$count . “<br>”;
recursive(–$count);
}
}
recursive(10);
?>
At Each execution of a function as a self-contained entity in memory. Even when called from within itself, that new call makes a copy of itself and any variable declarations made within that function stay within it.
