Flag

Summary

Use a variable as a flag. Set the flag if any of a number of things happens. After, check the flag to see if any of those things happened.

Situation

You want one code chunk to tell another code chunk what happened. For example, validation code tells processing code whether there were validation errors.

Needs

A variable you can use as a flag.

Provides

A variable you can test to see of one of a number of things happened.

Action

Initialize a variable. E.g.:

  • $isLifeOk = true;

If something happens you want to remember, change the flag.

  • if ($doggoCount == 0) {
  •     print "<p>Get some doggos!</p>\n";
  •     $isLifeOk = false;
  • }
  • ... More tests as required.

Check the flag after all the tests:

  • if (!$isLifeOk) {
  •     print "<p>Sorry your life sucks.</p>\n";
  • }