PHP how to set a one-off variable

A one-off variable makes a predicate expression true or false only the first time it's evaluated, the same expression will be opposite false or true ever after. This is useful when there something needs to be done only when the same block of code is executed on the first time. Usually the code block is in a loop, you can always check if it's the first loop over the block by looking at the loop index, but when that information is unavailable for example a foreach or the code block is included from another file, you need a one-off variable. You can use define and defined to do this as below.

          if( !defined('ONEOFF')) {
              define('ONEOFF', '');
              // do things for the first loop
          } else {
              // do things for the rest

The define defines a named constant which has global scope, so the code works even the code block is executed in a function gets invoked repeatedly.