Is it possible to use count ($ arr) in loop conditions

I wonder which way is better to use and why?

    $arr = array(); // some array
    for($i = 0; $i < count($arr); ++$i) {
        //some code
    }

or

    $arr = array(); // some array
    $cnt = count($arr);
    for($i = 0; $i < $cnt; ++$i) {
        //some code
    }

Thanks for your reply!

+4
source share
3 answers

I think the biggest difference is what you need to know in the first example if you are modifying an array.

As an example:

the first:

<?php

    $arr = [1,2,3];
     for($i = 0; $i < count($arr); ++$i) {
                    //^^^^^^^^^^^ See here
        echo "x". "<br />";
        array_pop($arr);
    }   

?>

Conclusion:

x
x

Condition: $i < count($arr)

1. Iteration: 0 < 3  -> TRUE
2. Iteration: 1 < 2  -> TRUE
3. Iteration: 2 < 1  -> FALSE

second:

<?php

    $arr = [1,2,3];
    $c = count($arr);
     for($i = 0; $i < $c; ++$i) {
                    //^^ See here
        echo "x". "<br />";
        array_pop($arr);
    }   

?>

Conclusion:

x
x
x

Condition: $i < $c

1. Iteration: 0 < 3  -> TRUE
2. Iteration: 1 < 3  -> TRUE
3. Iteration: 2 < 3  -> TRUE
4. Iteration: 3 < 3  -> FALSE

So, as you can see in the first example, the loop runs 1 iteration less than the other! What for? Because it checks the condition at each iteration, so when you change the original array in a for loop, the count will be different at each iteration, the opposite of the one in the second example, counting the array before the loop, and the variable will not change in the for loop.

, , , . ( , , count() )

, , 1 , , count().

:

number of ops:  11
compiled vars:  !0 = $arr, !1 = $i
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   INIT_ARRAY                                       ~0      
         1      ASSIGN                                                   !0, ~0
   4     2      ASSIGN                                                   !1, 0
         3  >   SEND_VAR                                                 !0
         4      DO_FCALL                                      1  $3      'count'
         5      IS_SMALLER                                       ~4      !1, $3
         6    > JMPZNZ                                        9          ~4, ->10
         7  >   PRE_INC                                                  !1
         8    > JMP                                                      ->3
   6     9  > > JMP                                                      ->7
        10  > > RETURN                                                   1

:

number of ops:  12
compiled vars:  !0 = $arr, !1 = $cnt, !2 = $i
line     # *  op                           fetch          ext  return  operands
---------------------------------------------------------------------------------
   3     0  >   INIT_ARRAY                                       ~0      
         1      ASSIGN                                                   !0, ~0
   4     2      SEND_VAR                                                 !0
         3      DO_FCALL                                      1  $2      'count'
         4      ASSIGN                                                   !1, $2
   5     5      ASSIGN                                                   !2, 0
         6  >   IS_SMALLER                                       ~5      !2, !1
         7    > JMPZNZ                                        A          ~5, ->11
         8  >   PRE_INC                                                  !2
         9    > JMP                                                      ->6
   7    10  > > JMP                                                      ->8
        11  > > RETURN                                                   1
+4

, . , , . . , -, (, ;)

0

count - , , . , , , .

0
source

All Articles