What is type hinting in PHP?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid PHP basics — a prerequisite for any developer role.
Answer
Type hinting (formally called type declarations in PHP 7+) allows you to specify the expected data type of function parameters, return values, and class properties. PHP 7 added scalar type hints: function add(int $a, int $b): int { return $a + $b; }. By default, PHP coerces values to the declared type (weak mode). Strict mode (enforces exact types with a fatal error for mismatches) is enabled with declare(strict_types=1); at the top of the file. PHP 8 added union types (int|string), nullable types (?int), the mixed, never, and void types, and PHP 8.1 added intersection types (Iterator&Countable). Type declarations make code self-documenting and catch type-related bugs early.
Common Mistake
Don't just define the term — demonstrate that you understand when to use it and when not to. Showing awareness of trade-offs is what separates average from strong PHP candidates.
Previous
What is the difference between array() and [] syntax in PHP?
Next
What is the null coalescing operator in PHP?