Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

strict_types

strict_types is an execution directive, that makes PHP more strict when applying the types. In particular, it doesn’t cast scalar values automatically, but raise an error.

strict_types is an option per file, and is set with the declare() function.

<?php

    declare(strict_types=1);
    
    function trumpet(int $a) {}

    trumpet(1);
    trumpet(1.3);
    //Fatal error: Uncaught TypeError: trumpet(): Argument #1 ($a) must be of type int, float given,

?>

Documentation

See Also