Jump to content

Running a line only once in a for loop


witt

Recommended Posts

In the example given by the OP it doesn't make sense to have the second statement in the loop, but in real code, it might. Let's say the for loop is looping through a large array of information and you need to execute a unique piece of code once if some condition is met by the value in one element of the array, then the question makes sense and the for loop would be coded something like this:
[code]<?php
$flag = false;
for ($i=0;$i<count($the_array);$i++) {
//
//  do some work
//
      if ($the_array[$i] == 'some condition' && !$flag) {
           echo 'The condition has been met'; // do the one line -- it doesn't have to be an echo
           $flag = true;
      }
//
//   do some more work
//
}?>[/code]
Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.