What is type hinting in PHP?
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.
Previous
What is the difference between array() and [] syntax in PHP?
Next
What is the null coalescing operator in PHP?