declare(%s) value must be a literal

Description

declare() directives (ticks, encoding, strict_types) are processed by the compiler while the file is being parsed, before any code actually runs. At that point, constants, variables, and function calls are not yet available, so only a literal value, such as 1, 0, or a quoted string, can be given as the directive’s value.

This applies equally to every directive: ticks, encoding, and strict_types all require a literal.

Example

<?php

define('TICK_VALUE', 1);

declare(ticks = TICK_VALUE) {
    echo 'Done';
}

?>

Literal Examples

  • declare(ticks) value must be a literal

  • declare(strict_types) value must be a literal

  • declare(encoding) value must be a literal

Solutions

  • Replace the constant, variable, or expression with its literal value.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.