title: PHP: What's the difference between static variable inside a for loop and a non-static variable outside the for loop?
body: <p>For example, what's the difference between</p>
  
  <pre><code>$foobar = 0
  for ($i=0; $i&lt;20; $i++) {
      //do something using $foobar
      $foobar++;
  }
  </code></pre>
  
  <p>and</p>
  
  <pre><code>for ($i=0; $i&lt;20; $i++) {
      static $foobar = 0
      //do something using $foobar
      $foobar++;
  }
  </code></pre>
  
  <p>???</p>
  
  <p>Both of the above examples have different results than from the following:</p>
  
  <pre><code>for ($i=0; $i&lt;20; $i++) {
      $foobar = 0
      //do something using $foobar
      $foobar++;
  }
  </code></pre>
  
  <p>All three variations have a different outcome. I understand that in the <em>first</em> of the three examples the value of the $foobar variable gets larger and larger and that in the <em>third</em> example the value of the $foobar variable gets reset during each loop. I'm not sure what's going on with the example using the static $foobar variable. It would seem that the first two examples should behave the same in the portion of the for loop where $foobar is used, but that is not the case for me.</p>
  
  <p>For reference, here's my actual code (the algorithm is not complete yet). I've marked the for() loop that has me thinking about this topic:</p>
  
  <pre><code>function combine($charArr, $k) {
  
      $currentsize = sizeof($charArr);
      static $combs = array();
      static $originalsize = "unset";
      if ($originalsize === "unset") $originalsize = $currentsize;
      static $firstcall = true;
  
      if ($originalsize &gt;= $k) {
  
          $comb = '';
          if ($firstcall === true) { 
              for ($i = $originalsize-$k; $i &lt; $originalsize; $i++) {
                  $comb .= $charArr[$i];
              }
              $combs[] = $comb; 
              $firstcall = false; 
          }
          if ($currentsize &gt; $k) { 
  
              $comb = ''; //reset
              for ($i=0; $i&lt;$k; $i++) { 
                  $comb .= $charArr[$i];
              }
              $combs[] = $comb;
  
              //########### THE FOR LOOP IN QUESTION ###########              
              for ($i = $k-1; $i &gt;= 0; $i--) { 
              static $range_adj = 0;
                  for ( $j = $i+1; $j &lt; $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 &gt; $k) { 
                  array_splice($charArr, 0, 1); 
                  combine($charArr, $k); 
              }
          }
          $output = array_values( $combs );
          unset($combs);
          return $output;
      }
      else {
          return false;
      }
  }
  </code></pre>
  
  <p>If I remove the <code>$range_adj</code> variable from for loop and place it right before the said for loop as a none-<code>static</code> variable, then the result of my function is <strong>not</strong> the same. Here's what the modified for loop would look like:</p>
  
  <pre><code>            $range_adj = 0;
              for ($i = $k-1; $i &gt;= 0; $i--) { 
                  for ( $j = $i+1; $j &lt; $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++;
              }
  </code></pre>
  
  <p>The fact that I get two different outcomes leads me to believe that something is different with each method, because if the two methods produced identical results, then the outcome of my function would be the same in both scenarios, which is not the case when I test these scenarios. Why am I getting two results? Test my function out with both methods of the for loop implemented and you will also get varying results.</p>
  
  <p>For your convenience, here's the original method:</p>
  
  <pre><code>            for ($i = $k-1; $i &gt;= 0; $i--) { 
                  static $range_adj = 0;
                  for ( $j = $i+1; $j &lt; $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++;
              }
  </code></pre>
  
  <p>???</p>
  
  <p>It seems to me that the results should be exactly the same, but they are not. If you run my function, you will notice that you get a different result with each method of the for loop.</p>