Question

So for example, I have a static variable inside a recursive function, and I want that variable to be static through out each call of the recursion, but once the recursion is finished, I want that variable to be reset so that the next time I use the recursive function it starts from scratch.

For example, we have a function:

<?php
function someFunction() {
    static $variable = null;
    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.
    return ($variable);
}
?>

We can call the function for the first time like this: someFunction(); and it will work fine. Then we call it again: someFunction(); but this time it starts with the previous value for $variable. How can we reset it after the recursion of the first time we called the function so that the second time we call it it is like starting over fresh?

Was it helpful?

Solution

Prodigitalsons answer is the best solution, but since you asked for a solution using static variables and I don't see an appropriate answer here's my solution.

Just set the static variable to null when you're done. The following will print 12345 on both calls.

function someFunction() {
    static $variable = 0;
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();

    $returnValue = $variable;
    $variable = null;
    return $returnValue;
}
someFunction();
echo "\n";
someFunction();
echo "\n";

Or combine this with the previous answer with an initializer:

function someFunction($initValue = 0) {
    static $variable = 0;
    if($initValue !== 0) {
        $variable = $initValue;    
    }
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();

    $returnValue = $variable;
    $variable = null;
    return $returnValue;
}

someFunction(2);
echo "\n";
someFunction(3);
echo "\n";
someFunction();
echo "\n";
someFunction(-2);

Will output:

345
45
12345
-1012345

OTHER TIPS

The simplest thing to do is pass the variable as an argument. I wouldnt really mess with static here.

function someFunction($value = null) {
    do stuff; change value of $value; do stuff;
    someFunction($value); # The value of $variable persists through the recursion.
    return $value;
}

As a general rule you should have to pass the arguments to the function (unless they operate on class properties within the same class)... they shouldnt be global and in the case of recursion its probably not a good idea to make them static... Treat a function like a black box... values go in... they get stuff done with/to them and a result comes out. They shouldnt be aware of things happening elsewhere. There are some exceptions, but IMO they are very few.

Ok, well I see prodigitalson squirreled me on the answer. Here's a demo:

http://codepad.org/4R0bZf1B

<?php
function someFunction($varset = NULL) {
    static $variable = NULL;
    if ($varset !== NULL) $variable = $varset;
    $variable++;
    echo $variable;
    if ($variable < 5) someFunction();
    return $variable;
}

someFunction(4);
echo "\n";
someFunction(2);
echo "\n";
someFunction(3);

?>

Outputs:

5
345
45

You can use a $depth counter :

function someFunction() {
    static $variable = null, $depth= 0;
    $depth++;

    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.

    $depth--;
    $temp = $variable;
    if($depth== 0){
        $variable = null;
    }
    return ($temp);
}
<?php
function someFunction() {
    static $variable = null;
    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.
    $variable = null;
    return ($variable);
}
?>

I found a solution:

<?php
function someFunction($clear_static = false) {
    static $variable = null;
    if ($clear_static) {
        $variable = null;
    }
    do stuff; change value of $variable; do stuff;
    someFunction(); # The value of $variable persists through the recursion.
    return ($variable);
}

someFunction(); # first manual call.
someFunction(); # second manual call, $variable has value from previous use.
someFunction(true); # third manual call, value of $variable starts like new.
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top