PHP 8 interview question · Most asked

What does `declare(strict_types=1)` do?

Summary

It makes scalar type declarations strict for calls made from that file, so passing '5' to an int parameter throws TypeError instead of being coerced.

Longer Answer

It is per-file and applies to the caller file: calls originating in a strict file are checked strictly, even into non-strict files. Return types are checked based on the file where the function is defined. The only allowed implicit conversion is int → float.

Key Points

  • Per-file, must be the first statement
  • Affects calls made from that file
  • Only int → float widening is allowed
  • Recommended for all new code

More most-asked PHP 8 questions

← All PHP 8 questions