Doubts increase $ i + loop for

1

Colleagues, sometimes I come across some snippets in the code used by the $ i variable before the loop ($ i = 0) and in the middle and the end $ i ++, as shown below:

Ex1)

$c = count($obj);
$i = 0; //aqui
for ($j = 0; $j < $c; $j++) {

    $matr = $obj[$j]->MATR;
    $nome = trim($obj[$j]->NOME);
    $cd_lot = trim($obj[$j]->LOTACAO);
    $nm_lot = $obj[$j]->DESC_LOTA;
    $regional = $obj[$j]->ESTAB;
    $cargo = $obj[$j]->CARGO;


if (($isAtivo) && (substr($cd_lot, 0, 1) == 'E') ) {
    $arrFolhaOrd[$i] = array();
    $arrFolhaOrd[$i]['nome'] = $nome;
    $arrFolhaOrd[$i]['matr'] = $matr;
    $arrFolhaOrd[$i]['cargo'] = $cargo;
    $i++; //aqui
    $arrFolha1[$matr] = array();
    $arrFolha1[$matr]['nome'] = $nome;
    $arrFolha1[$matr]['cd_lot'] = $cd_lot;
    $arrFolha1[$matr]['nm_lot'] = $nm_lot;

    }
}

Ex2:

while ($linha = mysql_fetch_array($rs)) {
$matr = $linha['matr'];
$cd_lot = trim($linha['cd_lot']);
$ap_freq = $linha['ap_freq'];
if ($matr_ant != $matr){
    $aAtuacao[$matr] = array();
    $i = 0;
    $matr_ant = $matr;
}
$aAtuacao[$matr][$i] = array();
$aAtuacao[$matr][$i]['cd_lot']=$cd_lot;
$aAtuacao[$matr][$i]['ap_freq']=$ap_freq;
$i++;

}

I would like some tips on how to use auto increment $ i ++ and $ i = 0, inside and outside the for and while loop. Thank you

    
asked by anonymous 03.07.2015 / 18:34

2 answers

2

In your for variable $i is being used to increment the value when it has the condition:

  • ($isAtivo) && (substr($cd_lot, 0, 1) == 'E') for true

Then after for if you want to know how many times you entered in if just do one:

echo $i;


In your while variable i is being used to populate an array while the condition:

  • $matr_ant != $matr for false

So long as it is the same, a new array is created at position $i within $aAtuacao[$matr] and filled with values. When this $matr_ant != $matr condition is true , that is, it is different then the $i index is reset to zero and starts the arrays from starting position.

The variable $i is used a lot because it is an abbreviation of the word i ncrement, that is, whenever you need to count or access positions in arrays is very common declare this variable to make the control.

    
03.07.2015 / 18:45
1

I think it all depends on the operation you want to perform.

Normally in loopbacks While declaring the variable $ i = 0 (or another value) before the loop begins, and within this repetition we can increment or decrement, as I have already said, everything will depend on the operation you want to perform, you may even need to change the value of it during the repeat trait to avoid an infinite loop for example.

Example: Adding 1 to 10.

$i = 0;
$soma = 0;
while ($i != 10) {
    $soma = $soma + $i;
    $i++;
}
echo $soma;

And in looping repetitions For we can declare the variable $ i = 0 before the loop repetition or directly when we mention For, in this type of operation I do not recommend changing the value of the variable $ i during the loop repetition. This is pretty much used to traverse Arrays.

Example: Traversing a vector and declaration within the variable in For, traverses vector values from 0 to vector size.

for ($i = 0; i < $vetor.length; i++){
    echo $vetor[i];
}

I recommend for and foreach to traverse Arrays, vectors, and arrays. While, Repeat for other operations.

Check out this article: link

    
03.07.2015 / 18:55