Having a foreach loop, is it possible to stop it if a certain condition becomes valid?
Example:
<?php
foreach ($foo as $bar) {
if (2+2 === 4) {
// Do something and stop the cycle
}
}
?>
I tried to use return and exit, but it didn’t work as expected, because I want to continue executing the remaining of the PHP script.
Use
break:Break will jump out of the
foreachloop and continue execution normally. If you want to skip just one iteration, you can usecontinue.