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
- What are the most important features introduced in PHP 8.0?
- What happens to an HTTP request in a PHP-FPM application, and what does 'shared nothing' mean?
- How should passwords be stored in PHP?
- What are named arguments and what are their benefits and risks?
- What is constructor property promotion?
- How does `match` differ from `switch`?
- What are union types in PHP 8 and how do you use them?
- What are enums in PHP 8.1? Explain pure vs backed enums.