Assuming I understand you correctly, I think you want to use "break" to stop the loop as soon as the problem is discovered.
if (Auth::LoggedIn() && Auth::$userinfo->rank == 'Student') { foreach ($allcourses as $course) { if ($course->aircraft == '1') { echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>'; break; } if ($course->aircraft == '2') { echo '<div class="msg-red">Some lessons could not be found, because you may not be entitled to view/book them at this stage of your course.</div><br/>'; break; } } }
Above, I also translated βif registeredβ conditional to be outside the loop (therefore, he only checked once).
Something to consider:
A more convenient approach is to add each error to the array - instead of using an echo and a break - and then skip this array of errors at the end, showing with additional error information so that they can be fixed once by the end user (depending on how your form works of course).
source share