Home > Articles

  • Print
  • + Share This
This chapter is from the book

Breaking Out of a Control Structure or Script

If you want to stop executing a piece of code, you can choose from three approaches, depending on the effect you are trying to achieve.

If you want to stop executing a loop, you can use the break statement as previously discussed in the section on switch. If you use the break statement in a loop, execution of the script will continue at the next line of the script after the loop.

If you want to jump to the next loop iteration, you can instead use the continue statement.

If you want to finish executing the entire PHP script, you can use exit. This approach is typically useful when you are performing error checking. For example, you could modify the earlier example as follows:

if($totalqty == 0){
  echo "You did not order anything on the previous page!<br />";

The call to exit stops PHP from executing the remainder of the script.

  • + Share This
  • 🔖 Save To Your Account