Home > Articles

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

Using declare

One other control structure in PHP, the declare structure, is not used as frequently in day-to-day coding as the other constructs. The general form of this control structure is as follows:

declare (directive)
{
// block
}

This structure is used to set execution directives for the block of code—that is, rules about how the following code is to be run. Currently, only two execution directives, ticks and encoding, have been implemented.

You use ticks by inserting the directive ticks=n. It allows you to run a specific function every n lines of code inside the code block, which is principally useful for profiling and debugging.

The encoding directive is used to set encoding for a particular script, as follows:

declare(encoding='UTF-8');

In this case, the declare statement may not be followed by a code block if you are using namespaces. We’ll talk about namespaces more later.

The declare control structure is mentioned here only for completeness. We consider some examples showing how to use tick functions in Chapters 25, “Using PHP and MySQL for Large Projects,” and 26, “Debugging and Logging.”

  • + Share This
  • 🔖 Save To Your Account