Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Keyword declare

Set a compilation or execution directive for the file or supported block. strict_types must appear at the start of the file and makes ordinary scalar argument checks depend on the calling file.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

declare(strict_types=1);

Set a compilation or execution directive for the file or supported block. strict_types must appear at the start of the file and makes ordinary scalar argument checks depend on the calling file. An integer still satisfies a float parameter. ticks and encoding serve different directive-specific purposes.

declare(strict_types=1);
function half(float $value): float { return $value/2; }
echo half(3),PHP_EOL;
try { half("3"); } catch(TypeError $e) { echo get_class($e); }
1.5
TypeError

Examples

<?php
declare(strict_types=1);
function half(float $value): float { return $value/2; }
echo half(3),PHP_EOL;
try { half("3"); } catch(TypeError $e) { echo get_class($e); }

Result

1.5
TypeError

Sources