Type declarations
Parameter, property, and return types express allowed values.
What it is
Parameter, property, and return types express allowed values.
When to use it
Use union and nullable types to make boundaries explicit.
Syntax
function find(int $id): ?User {}
function parse(string|int $value): int {}Example
function normalizeId(string|int $value): int
{
if (filter_var($value, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1]]) === false) {
throw new InvalidArgumentException("Invalid ID");
}
return (int) $value;
}Common mistakes
Types do not establish ranges or ownership; coercion varies with strict_types.