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 = '';
for ($i=0; $i<$k; $i++) {
$comb .= $charArr[$i];
}
$combs[] = $comb;
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.