1.2.328. Declare strict_types Usage

Usage of strict_types. By default, PHP attempts to change the original type to match the type specified by the type-declaration. With an explicit strict_types declaration, PHP ensures that the incoming argument has the exact type.

strict_types were introduced in PHP 7.0.

<?php

// Setting strict_types;
    declare(strict_types = 1);

    function foo(int $i) {
        echo $i;
    }

    // Always valid : displays 1
    foo(1);
    // with strict types, this emits an error
    // without strict types, this displays 1
    foo(1.7);

?>

See also declare.

1.2.328.1. Specs

Short name

Php/DeclareStrictType

Rulesets

All, Appinfo, CE, Changed Behavior, Preferences

Exakat since

0.12.1

PHP Version

With PHP 7.0 and more recent

Severity

Time To Fix

Precision

Very high

Features

declare

Available in

Entreprise Edition, Community Edition, Exakat Cloud