What is the difference between ending a loop with break; and exit(); in PHP?
Using `break;` in a loop will only exit the loop and continue executing the rest of the code outside the loop. On the other hand, using `exit();` will immediately terminate the script and stop all further execution. Therefore, if you want to just exit the loop and continue with the rest of the script, use `break;`. If you want to completely stop the script, use `exit();`.
// Using break; to exit the loop
for ($i = 0; $i < 10; $i++) {
if ($i == 5) {
break; // exit the loop
}
echo $i . "<br>";
}
// Using exit(); to completely stop the script
for ($i = 0; $i < 10; $i++) {
if ($i == 5) {
exit(); // stop the script
}
echo $i . "<br>";
}