PHP: What is the difference between a static variable inside a for loop and a non-static variable outside a for loop?

For example, what's the difference between

$foobar = 0
for ($i=0; $i<20; $i++) {
    //do something using $foobar
    $foobar++;
}

and

for ($i=0; $i<20; $i++) {
    static $foobar = 0
    //do something using $foobar
    $foobar++;
}

???

Both of the above examples have different results than the following:

for ($i=0; $i<20; $i++) {
    $foobar = 0
    //do something using $foobar
    $foobar++;
}

All three options have a different result. I understand that in the first of the three examples, the value of the $ foobar variable becomes more and more, and in the third example, the value of the $ foobar variable gets reset during each cycle. I'm not sure what happens with the example using the static $ foobar variable. It would seem that the first two examples should behave identically in the part of the for loop where $ foobar is used, but this does not apply to me.

For reference, here is my actual code (the algorithm is not complete yet). I noted a for () loop that makes me think about this topic:

function combine($charArr, $k) {

    $currentsize = sizeof($charArr);
    static $combs = array();
    static $originalsize = "unset";
    if ($originalsize === "unset") $originalsize = $currentsize;
    static $firstcall = true;

    if ($originalsize >= $k) {

        $comb = '';
        if ($firstcall === true) { 
            for ($i = $originalsize-$k; $i < $originalsize; $i++) {
                $comb .= $charArr[$i];
            }
            $combs[] = $comb; 
            $firstcall = false; 
        }
        if ($currentsize > $k) { 

            $comb = ''; //reset
            for ($i=0; $i<$k; $i++) { 
                $comb .= $charArr[$i];
            }
            $combs[] = $comb;

            //########### THE FOR LOOP IN QUESTION ###########              
            for ($i = $k-1; $i >= 0; $i--) { 
            static $range_adj = 0;
                for ( $j = $i+1; $j < $currentsize-$range_adj; $j++ ) { 
                    if ( !($i == 0 and $j == $currentsize-$range_adj-1) ) { 
                        $comb = substr_replace($comb, $charArr[$j], $i, 1); 
                        $combs[] = $comb;
                    }
                }
                $range_adj++;
            }
            if ($currentsize-1 > $k) { 
                array_splice($charArr, 0, 1); 
                combine($charArr, $k); 
            }
        }
        $output = array_values( $combs );
        unset($combs);
        return $output;
    }
    else {
        return false;
    }
}

$range_adj for for none-static, . , loop:

            $range_adj = 0;
            for ($i = $k-1; $i >= 0; $i--) { 
                for ( $j = $i+1; $j < $currentsize-$range_adj; $j++ ) { 
                    if ( !($i == 0 and $j == $currentsize-$range_adj-1) ) { 
                        $comb = substr_replace($comb, $charArr[$j], $i, 1); 
                        $combs[] = $comb;
                    }
                }
                $range_adj++;
            }

, , , - , , , , . ? for, .

:

            for ($i = $k-1; $i >= 0; $i--) { 
                static $range_adj = 0;
                for ( $j = $i+1; $j < $currentsize-$range_adj; $j++ ) { 
                    if ( !($i == 0 and $j == $currentsize-$range_adj-1) ) { 
                        $comb = substr_replace($comb, $charArr[$j], $i, 1); 
                        $combs[] = $comb;
                    }
                }
                $range_adj++;
            }

???

, , . , , for.

+3
4

, . , , , static , . , . , $foobar - , /; PHP .

, $foobar : , , undefined - .

+2

static $foobar = 0;
, static $foobar = 0 $foobar

+1

, , reset . "" , , .. .

0

static , .

, , , . PHP , .

: http://codepad.org/evilks4R

, , , , , , , , .

static, , .

here is an example of passing static members to the last passes of a function call: http://codepad.org/gChjx7Om

if you did this:

$foo_bar = 0;
for(.....)

this would be within the function, so that $foo_barit would get reset to 0 every time the function was called, so it would rewrite the calculations of the last calls.

if you moved $foo_baroutside the function, and then, when necessary, into the function you added:

global $foo_bar

You will have the same results.

http://www.cprogramming.com/tutorial/statickeyword.html

0
source

Source: https://habr.com/ru/post/1795031/


All Articles