foreach ( $names as &$name ) to make every name in a list uppercase, in place. Right after that loop finishes, a second, completely unrelated foreach ( $names as $name ) prints the list. The very last name now appears twice, and one of the original names is missing entirely. The second loop's code looks perfectly normal on its own. What actually happened?&$name, by reference, so once that loop ends, $name is left pointing directly at the array's last element, rather than being a fresh, ordinary variable again. The second loop reuses the exact same name, $name, without &. On each of its own passes, the value it assigns to $name still overwrites the array's true last element too, through that leftover reference, which is why the last item gets duplicated and an earlier one quietly disappears.foreach loop is built specifically for going through every item in an array, one at a time, without needing to track a position or count anything manually.while loop repeats its block for as long as its condition stays true, checked again fresh before every single pass, including the very first one. Nothing about a while loop guarantees it will ever run a fixed number of times.for loop combines a starting point, a condition, and a step, such as increasing a counter, into one line, which is why it is commonly used whenever the number of repeats is already known or easily counted.break stops a loop completely and immediately, skipping every remaining item or repeat. continue only skips the current pass and moves straight on to the next one, leaving the loop itself still running.continue skips only the current pass through the loop, jumping straight to the next item in $scores. It does not stop the loop, which is why scores below 50 are simply left out of $passing rather than ending the whole process.break stops the loop entirely, the moment count( $passing ) === 3 becomes true. Any remaining scores in the array, even ones that would have passed, are never even looked at after that point.$i++, inside the while loop, is what eventually makes $i < count( $passing ) false. Removing that single line would not produce fewer repeats. It would make the loop run forever, since nothing else in the loop changes $i.$passing[] = $score; appends $score onto the end of $passing and grows the array by exactly one item, without needing to know how many items are already in it.count( $passing ) is called fresh every single time the while condition is checked, not just once before the loop starts. If an item were added to $passing inside the while loop's own body, the loop's stopping point would move along with it.continue here only skips one failing score and keeps the loop running, producing two passing scores from the first four before the early exit at three. Writing break instead of continue at that exact spot would stop the loop the first time a low score appears, producing far fewer passing scores, from a one-word difference.$i++ from the while loop does not slow it down or shorten it. It makes $i < count( $passing ) stay true forever, since $i never changes, and the page, or the entire server process handling it, simply never finishes responding.foreach ( $array as &$value ) leaves $value pointing at the array's last element once the loop finishes. Reusing that same variable name in a later, ordinary foreach ( $array as $value ) without & can then overwrite that last element again on every later pass, through the leftover reference, producing a duplicated final value and a silently lost earlier one.count( $passing ) inside the while condition recalculates the array's length on every single pass. That is fine for a short list like this one, but doing the same thing inside a loop running tens of thousands of times, on a much larger array, adds that recount cost tens of thousands of times instead of once.while loop in this code could be rewritten as for ( $i = 0; $i < count( $passing ); $i++ ), combining the start, the condition, and the step into one line. Writing it as separate while and $i++ statements instead means a missing or misplaced $i++ anywhere in the loop's body is a mistake the for version's structure makes much harder to make by accident.foreach loop that used & get followed by unset() on that same loop variable right afterward. Second, does a while loop's own condition variable actually get changed somewhere inside the loop's body, not just declared once before it starts. Third, does a function such as count(), recalculated inside a loop's own condition, need to be moved into a variable set once before the loop, for a large array.unset( $name ) line to the bad_code example, in the right place, and explain why this one line fixes the duplicate output.$i < count( $passing ) but never changing $i anywhere inside the loop, producing a page that never finishes loading.$i, for both, so the inner loop's changes to $i quietly affect how many times the outer loop continues to run.foreach ( $array as $index => $value ) will always start at 0 and count up by one each time, which is only true for an array built that way. An array with custom or non-numeric keys keeps those same keys all the way through the loop.Welcome back to Jit4All