Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Type Inference

Type inference is the ability of a tool, such as a static analyzer or the PHP engine, to deduce the type of an expression, variable, or return value without an explicit type declaration.

PHP itself does not infer types for local variables: their type can only be declared on function arguments, return values, properties, and constants. However, static analyzers perform type inference by tracking assignments, control-flow branches, and function signatures to determine what type a value holds at every point in the code.

Type inference enables early detection of type errors even in partially or untyped code. It is the engine behind gradual type checking: the tool fills the gaps left by missing annotations. PHPDoc blocks, @var, @param, @return, are additional hints that guide inference when the code alone is ambiguous.

A classic example is narrowing: after an is_string() guard, the analyzer infers that the variable is a string inside the if-branch, removing false-positive warnings.

<?php

    function process(mixed $value): string {
        if (is_string($value)) {
            // type inferred as string here
            return strtoupper($value);
        }
        // type inferred as int|float|bool|array|object|null here
        return (string) $value;
    }

?>

Documentation

See Also