What are the best practices for ending loops in PHP scripts?
When ending loops in PHP scripts, it is best practice to use the "break" statement to exit the loop when a certain condition is met. This helps prevent infinite loops and ensures that the loop stops executing when necessary.
// Example of using the "break" statement to end a loop
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo $number . "\n";
if ($number == 3) {
break; // exit the loop when $number is 3
}
}