$results is an array, then I have:

for ($i = 0; $i < count($results) ; $i++) {
// do something
}

in this loop I make sure that all array elements are processed for something.
However the code above works fine, but I am not sure if having count() in a for loop is professional? is it fine to have count() in a for loop?

    That would depend on how $results is populated. I'm not sure if there's any performance difference on large arrays, but I normally do:

    for($i=0; isset($array[$i]); $i++) {

    If the keys of $results aren't automatically generated, however, then you may need to adjust the loop.

      Generally you don't want to include function calls within a loop. The code you have can be done as such.

      $length = count($results);
      for ($i = 0; $i < $length ; $i++) {
      // do something
      }
      

      This means there's only one call to count. Hope that helps.

        how would be something like this?

        for( $i=0, $t = count($array); $i < $t; $i++) {
        // do something
        }

        isn't it better?

          Assuming the goal is to loop through an array, you're probably better off using:

          foreach($array as $ix => $value)
          {
             // so something with $value, or with $array[$ix] if you need to actually
             // change this array value
          }
          

            no foreach is not suitable for what I want to use. please tell me my code above is better or the one given by joe_C_nice ?

              AndreF wrote:

              no foreach is not suitable for what I want to use.

              Why not? It allows you to cycle through each element of the array. If you are not sure if the elements are in array key order, do a [man]ksort/man first, e.g.:

              ksort($array, SORT_NUMERIC);
              foreach($array as $i => $value)
              {
                 echo "$i: $value<br>\n";
              }
              

              please tell me my code above is better or the one given by joe_C_nice ?

              If you really feel the need to use a for() loop, then:

              $t = count($array);
              for($i = 0; $i < $t; $i++)
              {
                 echo "$i: {$array[$i]}<br>\n";
              }
              

              The main problem with such a for loop though is that nasty things start to happen if for some reason there is any gap in the array index numbering, whereas that is never a problem with the foreach method.

                NogDog wrote:
                // so something with $value, or with $array[$ix] if you need to actually
                   // change this array value 

                If you're not interested in the key at all you can also write

                foreach($array as &$element)
                {
                   $foo = $element;
                   $element = strtoupper($foo);
                   // etc.
                }

                to access each element of the array for both reading and storing.

                But there is a gotcha to it: the $element variable remains in scope after the loop, and it is still a reference (to the last element in the array). So you need to unset() it after the loop, in case you have the urge to use the same variable later on in the function (which would otherwise end up clobbering the last element of the array).

                  Incidentally, (assuming as ever that the size of the array doesn't change within the loop):

                  for( $i=0, $t = count($array); $i < $t; $i++) {
                  // do something
                  }
                  

                  Is much better than

                  for( $i=0; $i < count($array); $i++) {
                  // do something
                  }
                  

                  because the latter needs to re-evaluate count($array) on every iteration, while the former only needs to do it once, at the start. If count($array) (or any other expression, for that matter) doesn't change in the course of the loop then it is wasteful to be doing it inside the loop.

                    Write a Reply...