How To Create a Code Block in PHP for Error Handling

Published Date Author: , Posted October 19th, 2012 at 3:42:20pm

The do/while statement is sometimes used to break out of a block of code when an error condition occurs. For example:

1
2
3
4
5
6
do {
  // do some stuff
  if ($error_condition)
    break;
  // do some other stuff
} while (false);
do {
  // do some stuff
  if ($error_condition)
    break;
  // do some other stuff
} while (false);

Because the condition for the loop is false, the loop is executed only once, regardless of what happens inside the loop. However, if an error occurs, the code after the break is not evaluated.

No comments as yet.

Leave Your Comment  Leave a comment

All fields marked with "*" are required.