Doubt increment $i + loop for

Asked

Viewed 740 times

1

Colleagues, sometimes I come across some snippets in the code used the variable $i before the repeat loop ($i = 0) and in the middle and end $i++, as the examples 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 and when to use auto increment $i++ and $i=0, inside and outside of the loop for and while. thank you

2 answers

2

In his for the variable $i is being used to increment the value when you have the condition:

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

Then after the for if you want to know how many times entered the if just make a:

echo $i;


In his while the variable i is being used to fill a array as long as the condition:

  • $matr_ant != $matr for false

Then as long as it is equal a new array is created at the position $i within the $aAtuacao[$matr] and filled with values. When this condition $matr_ant != $matr stays true, that is, it is different then the index $i is reset to zero and begins the arrays of initial position.

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

1

I believe it all depends on the operation you wish to perform.

Normally in loops While we declare the variable $i = 0 (or other value) before starting the loop of repetition and within this loop we can increment or decrease, as I said, everything will depend on the operation you want to perform, you may even need to change its value during the repeat stroke to avoid an infinite loop for example.

Example: Summing from 1 to 10.

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

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

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

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

I recommend for and foreach to go through arrays, vectors and matrices. While, Repeat for other operations.

Check out this article: http://feloliveira.com.br/blog/estruturas-de-repeticao-em-php-while-do-while-for-e-foreach/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.