PHP Type Declarations and declare(strict_types=1)

PHP’s type declarations let you enforce parameter and return types, but by default PHP still coerces values to match — declare(strict_types=1) turns that off entirely, catching a real category of bugs at the boundary.

<?php
declare(strict_types=1); // must be the very first statement in the file

function calculateTotal(float $price, int $quantity): float {
    return $price * $quantity;
}

calculateTotal(9.99, 3);      // fine
calculateTotal("9.99", 3);    // TypeError - no coercion allowed in strict mode

Without Strict Types

// Without declare(strict_types=1), PHP silently coerces:
calculateTotal("9.99", "3"); // works - strings quietly converted to float/int

Coercive mode (the default) feels convenient until a typo passes a string where you meant an integer and PHP silently “fixes” it instead of telling you something’s wrong. strict_types is a one-line change worth adding to every new file.