Cheat Sheets & Quick References

PHP Type Declarations Cheat Sheet

A concise guide to PHP parameter types, return types, union types, nullable types and strict typing.

Overview

Type declarations make function and method contracts clearer and help catch invalid values. PHP supports parameter types, return types, property types and several compound type forms.

Common forms

  • Scalar types include int, float, string and bool.
  • Class and interface names constrain object values.
  • Nullable types use ?Type where supported by the target PHP version.
  • Union types allow one of several declared types.
  • Intersection types express values that satisfy multiple class or interface types.
  • mixed accepts any value; use it deliberately.

Strict typing

declare(strict_types=1) affects scalar type coercion for calls made from the file where it is declared. Understand its scope and test public interfaces carefully.

Compatibility

Check the minimum PHP version for every syntax feature used by the project.

Official resource

PHP Type Declarations