Content · Web Development · PHP

PHP Strict Types: What declare(strict_types=1) Changes

Strict types affects eligible scalar parameter and return type checks within a source file. Object interfaces and array types already have their own type enforcement.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Strict types affects eligible scalar parameter and return type checks within a source file.

What It Does


Strict types affects eligible scalar parameter and return type checks within a source file. The following examples distinguish a normal use case, a boundary case and a practical application.

Requirements and Compatibility


PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.

The strict_types declaration applies in the calling file for scalar parameter typing; it does not perform business validation or replace explicit input parsing.

The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.

Example 1: Basic Behavior


Start with the smallest meaningful working case.

<?php
declare(strict_types=1);
function units(int $count): int { return $count * 2; }
try { units('3'); } catch (TypeError $e) { echo 'Rejected string at strict call site'; }

Example 2: Another Case


Examine the edge case and the observable result.

<?php
declare(strict_types=1);
function subtotal(float $price): float { return $price * 2; }
echo subtotal(5); // An int can be accepted for float

Practical Example: Putting It to Use


Strict mode does not prohibit the special int-to-float widening rule.

<?php
declare(strict_types=1);
function price(float $p): float { return $p * 2; }
echo price(5); // int is still accepted where float is declared

Important Behavior and Edge Cases


Object interfaces and array types already have their own type enforcement behavior.

Comparison with Related Functions or Approaches


OptionWhat it means in practice
Strict Types: What declare(strict_types=1) ChangesStrict types affects eligible scalar parameter and return type checks within a source file.
Related approachInspect both the calling file and declared function signature.

Common Beginner Mistakes


MistakeCorrect approach
Ignoring the important boundary for Strict Types: What declare(strict_types=1) ChangesObject interfaces and array types already have their own type enforcement behavior.
Assuming a related mechanism has the same behaviorInspect both the calling file and declared function signature.

Key Takeaways


  • Strict types affects eligible scalar parameter and return type checks within a source file.
  • Object interfaces and array types already have their own type enforcement behavior.
  • Inspect both the calling file and declared function signature.

Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.

Share this resourceSend this page to someone who may find it useful.