User Contributed Notes 18 notes

Just a note about using the continue statement to forego the remainder of a loop - be SURE you're not issuing the continue statement from within a SWITCH case - doing so will not continue the while loop, but rather the switch statement itself.

While that may seem obvious to some, it took a little bit of testing for me, so hopefully this helps someone else.

$array = $save;test_time("",1);reset($array);while ($next = next($array)){ $key = key($array); $array[$key] = $array[$key] * 100;} test_time("while (\$next = next(\$array))");*********************************************************The improvement seems huge, but it isnt that dramatic in real practice. Results varied... I have a very long bidimensional array, and saw no more than a 2 sec diference, but on 140+ second scripts. Notice though that you lose control of the $key value (unless you have numeric keys, which I tend to avoid), but it is not always necessary.

I generally stick to foreach. However, this time, I was getting Allowed Memory Size Exceeded errors with Apache. Remember foreach copies the original array, so this now makes two huge 2D arrays in memory and alot of work for Apache. If you are getting this error, check your loops. Dont use the whole array on a foreach. Instead get the keys and acces the cells directlly. Also, try and use unset and Referencing on the huge arrays.

Working on your array and loops is a much better workaround than saving to temporary tables and unsetting (much slower).

Just a note to stuart - the reason for this behaviour is because using the while(value = each(array)) construct increments the internal counter of the array as its looped through. Therefore if you intend to repeat the loop, you need to reset the counter. eg:

Therefore if you are creating a form to select data from database, and want the form displayed when search is done to show what parameters have been chosen that will do the trick !!

Let's say I make a search between different sports, I choose football in my form, send my query... then displays are show, the menu will have football selected because of the ternary operator that displays "selected>" on the <option> ;) Enjoy ^^

I find it often clearer to set a simple flag ($finished) to false at the start of the loop, and have the program set it to true when it's finished doing whatever it's trying to do. Then the code is more self-documenting: WHILE NOT FINISHED keep going through the loop. FINISHED EQUALS TRUE when you're done. Here's an example. This is the code I use to generate a random filename and ensure that there is not already an existing file with the same name. I've added very verbose comments to it to make it clear how it works:

> So in effect the main while loop is only doing one iteration... and not 4 as expected....

That's the wrong conclusion. The outer "while" does all four iterations. However the "inner" loop does nothing for the second, third and fourth run.

> I think it would be good to have an explaination of this strange behaviour.

Here it is:

<?PHP$i=0;while($i < count($one)) {

while($a = each($two)) { echo $a[1]." - ".$one[$i].", "; }$i++;

}?>

The "problem" is your use of "each", which reached the last item after the first iteration of the outer loop. After that, when you come back to the second iteration with the outer loop, "each" still is at the end of the array $two.

If you add a reset($two) in front of the inner "while", you'll get the result you expect.

function write_fix ($lock_file) { while( file_exists($lock_file){// do something in here? // maybe sleep for a few microseconds // to maintain stability, if this is going to // take a while ?? [just a suggestion]} }

?>

This method is not recommended for use with programs that will be needing a good few seconds to write to a file, as the while function will eat up alot of process cycles. However, this method does work, and is easy to implement. It also groups the writing functions into one easy to use function, making life easier. :-)