Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP declare statement

Reference for PHP declare: enable strict_types for scalar type checks, the ticks and encoding directives, where declare must appear, and its scope rules.

declare sets an execution directive for a block of code or for a whole file. PHP defines three directives: strict_types, ticks and encoding. By far the most important is strict_types=1, which makes calls and returns in that file use strict scalar type checking instead of coercion.

declare(strict_types=1); must be the very first statement in the file (only comments, whitespace and the opening tag may precede it). It affects calls made from that file and return values of functions defined in it.

PHP
<?php
declare(strict_types=1);

Try it

PHP strict_types in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP ticks and a declare block

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
declare(strict_types=1);
declare(ticks=1);
declare(ticks=1) { /* statements */ }
declare(ticks=1): /* statements */ enddeclare;
declare(encoding='ISO-8859-1');
Directive Description
strict_types 1 enables strict scalar type checking for calls made in this file; 0 (default) allows coercion. Must be the first statement; block form is not allowed.
ticks Number of low-level statements after which a tick event fires; handlers are registered with register_tick_function().
encoding Declares the script's encoding. Rarely useful and only honored when PHP is built with multibyte support and the zend.multibyte setting enabled.

Return value: none.

Errors: a fatal error if strict_types is not the first statement, uses a value other than 0 or 1, or is used in block form.

Usage notes

  • Per-file, not global. strict_types governs how the file's own calls pass arguments. A strict file can still call a function defined in a non-strict file, but the call is checked strictly. Return-type checks follow the file where the function is defined.
  • What strict mode changes. An int is accepted where float is expected (widening), but "5" for int, 5 for string or 1.5 for int throw TypeError. Built-in functions are checked too.
  • Recommended default. Most modern projects and frameworks put declare(strict_types=1); at the top of every file.
  • Ticks are rarely used. They cost performance and the exact tick count depends on the engine, so never rely on the number.
  • Not influenced by the interpreter. You cannot enable strict types globally with a setting.
  • Block form is allowed for ticks; the directive then applies only inside the block.
  • Conversions inside the file (arithmetic, string interpolation, casts) are unaffected; only function argument/return checks change.