W3docs

declare

The "declare" keyword is a control structure in PHP that allows you to set certain directives for a block of code. These directives affect the behavior of the

The PHP declare Keyword

declare is a PHP control structure that sets an execution directive for the code it governs. Unlike most statements, a directive does not run logic — it changes how the engine interprets the surrounding code. PHP supports exactly three directives: strict_types (enforces type declarations), encoding (sets the script's character encoding), and ticks (a legacy event hook, removed in PHP 8.0).

The most common reason to reach for declare today is a single line at the top of a file: declare(strict_types=1);. This page covers the syntax, each directive, and the gotchas that trip people up.

Syntax

There are two forms. The statement form applies the directive to the rest of the file:

declare(directive);

The block form applies the directive only to the code inside the braces:

declare(directive) {
    // code block
}

The block form is only valid for ticks. strict_types and encoding must use the statement form and must be the very first statement in the file (only <?php may precede them). Using the block form with strict_types is a fatal error.

strict_types: enforcing type declarations

By default, PHP coerces scalar arguments to match a function's type declarations. With declare(strict_types=1); it does not — passing the wrong type throws a TypeError. This is the directive you will actually use day to day.

php— editable, runs on the server

Two rules to remember:

  • It must be the first statement in the file. Anything before it — even whitespace output or another statement — is a fatal error.
  • It is per-file. Strict mode applies to calls made from this file, based on where the call lives, not where the function is defined. A library function called from a strict file is checked strictly; the same function called from a non-strict file is not.

Without the directive, add(2, "3") would silently coerce "3" to 3 and return 5 — which is exactly the kind of hidden bug strict_types is designed to surface.

encoding: declaring the script's character set

declare(encoding='ISO-8859-1');

This tells the engine how to interpret the bytes of the script file itself. It is rarely needed on modern systems, which use UTF-8 everywhere, but can matter for legacy files or specific server configurations. It only has an effect when PHP is built with multibyte support.

ticks: a legacy event hook

A tick is an event fired every N low-level statements executed inside the declare block. It was used to bolt simple callbacks (signal handling, profiling) onto otherwise synchronous code. This is the one directive where the block form is meaningful:

declare(ticks=1) {
    function tick_handler() {
        echo "tick" . PHP_EOL;
    }
    register_tick_function('tick_handler');

    $a = 1;
    $a += 2;
}

The exact number of ticks depends on how many statements run. The ticks directive was removed in PHP 8.0 — avoid it in new code; modern alternatives include pcntl_signal for signal handling.

When to use each directive

  • strict_types=1 — put it at the top of every new file. It is the single most valuable use of declare and a near-universal convention in modern PHP. See PHP Data Types and PHP Functions for how type declarations work.
  • encoding — only when you genuinely have a non-UTF-8 source file and multibyte support compiled in.
  • ticks — don't; it's gone as of PHP 8.0.

Conclusion

declare sets execution directives for a script. In practice that means one line — declare(strict_types=1); — which turns silent type coercion into explicit errors and makes your function signatures meaningful. The encoding and ticks directives are niche and legacy respectively, but knowing all three keeps the syntax (statement form vs. block form) from surprising you.

Practice

Practice
Which statements about the PHP declare keyword are true?
Which statements about the PHP declare keyword are true?
Was this page helpful?