PHP theorytheory 0/50 · 0%
Fundamentals · easy

19. Type Declarations

Strengthening function contracts.

PHP allows scalar and class type hints on parameters and return types, e.g. function add(int $a, int $b): int. Declaring `declare(strict_types=1);` at the top of a file enforces strict type checking instead of coercion.

Nullable types are written with a leading question mark, such as ?string, allowing null in addition to the declared type.

Check your understanding

  1. 1. What enables strict type checking in a file?

  2. 2. How do you declare a nullable string parameter?