enddeclare
Learn how PHP enddeclare closes a declare() block in alternative colon syntax, with runnable ticks examples and pitfalls to avoid.
Introduction
enddeclare is the closing keyword for a declare() block written in PHP's alternative syntax — the colon-based form that PHP offers as a substitute for curly braces. Just as if: is closed by endif and foreach: is closed by endforeach, declare(...): is closed by enddeclare;.
This page covers what the keyword actually does, the one directive it is realistically used with (ticks), the exact syntax PHP accepts, and the mistakes that produce a parse error.
When you actually need enddeclare
A declare() directive sets an execution behavior for a region of code. It has three directives: ticks, strict_types, and encoding. Of these:
strict_typesandencodingmust be the very first statement in a file and use the statement formdeclare(strict_types=1);— they have no block and therefore never useenddeclare.ticksis the only directive whose effect is naturally scoped to a block of statements, so it is the one you pair with the alternative syntax andenddeclare.
In practice that means: you reach for enddeclare when you want a ticks block and prefer colon syntax over braces. This is rare in modern code, which is exactly why the syntax is worth pinning down precisely.
Syntax
The alternative form replaces the opening { with a colon and the closing } with enddeclare;:
declare(directive=value):
// statements
enddeclare;The equivalent brace form is:
declare(directive=value) {
// statements
}The two forms are interchangeable. Note that
declare(...) { ... } enddeclare;(braces andenddeclare) is not valid —enddeclarebelongs only to the colon form. Mixing them is a syntax error.
Example: a ticks block closed with enddeclare
A tick is an event PHP emits every N low-level statements executed inside a declare(ticks=N) block. Registering a handler with register_tick_function() lets you run code on each tick — useful for lightweight profiling or signal-style hooks.
<?php
$ticks = 0;
register_tick_function(function () use (&$ticks) {
$ticks++;
});
declare(ticks=1):
$x = 1;
$y = 2;
$z = $x + $y;
enddeclare;
echo "Result: $z\n";
echo "Ticks fired: $ticks\n";Output:
Result: 3
Ticks fired: 3The handler runs once per statement inside the block (three assignments → three ticks). Code outside the declare(...): ... enddeclare; block does not trigger ticks.
Common pitfalls
Don't combine braces with enddeclare
This is the single most common mistake (and what older tutorials get wrong):
// ❌ Parse error: unexpected token "enddeclare"
declare(ticks=1) {
// ...
} enddeclare;Pick one style — either { ... } or : ... enddeclare;.
Don't use enddeclare with strict_types or encoding
declare(strict_types=1) and declare(encoding='UTF-8') are file-level directives that take no block, so there is nothing for enddeclare to close:
<?php
declare(strict_types=1); // statement form — no block, no enddeclarestrict_types must be the first statement in the file; placing it after any output or code is a fatal error.
Match every declare(...): with one enddeclare;
Like all alternative-syntax keywords, an opened declare(...): must be closed by exactly one enddeclare;. Forgetting it leaves the block unterminated and PHP reports an "unexpected end of file" error.
Conclusion
enddeclare closes a declare(...): block written in PHP's alternative syntax. The only directive you would realistically use it with is ticks, because strict_types and encoding are file-level statements with no block. Keep the colon and enddeclare; together and never mix them with curly braces, and the keyword behaves predictably.
For the full picture of execution directives, see declare; for the colon-based alternative syntax across control structures, see PHP Syntax.