Error messages as a flag

Summary

Use a variable containing error messages as a flag. If it's MT, no errors.

Situation

You want to tell the user when something goes wrong, e.g., in validation.

Action

Make a variable for error messages.

  • $errorMessage = '';

Set the variable if something goes wrong.

  • if (!$isUserLikesChocolate) {
  •     $errorMessage = "Huh?! You must not be human. Alien attack! Alien attack!";
  • }

After all the tests, if the variable is still MT, something went wrong.

  • if ($errorMessage != '') {
  •     print "<p>$errorMessage</p>;
  • }
  • else {
  •     // All OK, continue processing.
  •     ...
  • }