How can you efficiently replace content within a running while loop in PHP?

When replacing content within a running while loop in PHP, you can efficiently achieve this by using a variable to store the new content and then updating the loop variable with the new content. This allows you to dynamically replace content without interrupting the loop's execution.

$items = ['apple', 'banana', 'cherry'];

$replacement = 'orange';

foreach ($items as $item) {
    if ($item === 'banana') {
        $item = $replacement;
    }
    
    echo $item . PHP_EOL;
}