๐Ÿงฎ PHP Functions
Estimated reading: 3 minutes 368 views

PHP Type Hints โ€“ Enforce Data Integrity with Typed Parameters and Returns

Learn how to use type hints in PHP to define strict parameter and return types, improve code reliability, and reduce bugs.


Introduction โ€“ What Are Type Hints in PHP?

Type hints in PHP allow you to specify the expected data types for function parameters, return values, and class properties. Introduced in PHP 5 and improved in PHP 7+, type hints enhance code clarity, help catch type-related bugs, and enable type-safe programming.

In this guide, youโ€™ll learn:

  • How to apply type hints to function arguments and return values
  • Available scalar and class types
  • Nullable, union, and mixed types
  • Best practices and performance tips

1. Basic Parameter Type Hints

function greet(string $name) {
    echo "Hello, $name!";
}

greet("Alice"); // Hello, Alice!

Ensures the function only accepts a string.
Passing another type will throw a TypeError in strict mode.


2. Return Type Declarations (PHP 7+)

function add(int $a, int $b): int {
    return $a + $b;
}

echo add(2, 3); // 5

The function must return an int.
Mismatched types will cause a fatal error.


3. Scalar Type Hints (PHP 7+)

TypeDescription
intInteger (1, 2, 100)
floatDecimal/Double (3.14)
stringText (e.g., “hello”)
boolBoolean (true, false)

4. Class and Interface Type Hints

class User {}

function showUser(User $u) {
    echo "User provided";
}

showUser(new User());

PHP ensures only User objects can be passed.
Useful for dependency injection and OOP design.


5. Array and Callable Hints

function apply(array $items) {
    print_r($items);
}

function run(callable $func) {
    $func();
}

array and callable are special types supported for structure and functions.


6. Nullable Types (PHP 7.1+)

function greet(?string $name) {
    echo $name ? "Hi, $name" : "Hello, Guest";
}

greet(null); // Hello, Guest

Prefix the type with ? to allow null values.


7. Union Types (PHP 8.0+)

function format(int|string $data) {
    echo $data;
}

format(123);
format("Text");

Accepts multiple types.
Safer alternative to mixed or any type logic.


8. Mixed Type (PHP 8.0+)

function handle(mixed $input) {
    var_dump($input);
}

mixed allows any type โ€” like a dynamic type hint.
Use cautiously when flexibility is required.


Best Practices

  • Always use type hints for better readability and debugging
  • Combine with return types for full function typing
  • Use union or mixed types sparingly and document them
  • Validate null where applicable, or use ?type
  • Avoid type juggling โ€” prefer strict types when possible

Summary โ€“ Recap & Next Steps

PHP type hints help enforce type safety in your code by specifying what a function expects and returns. It leads to cleaner, more predictable, and less error-prone code.

Key Takeaways:

  • Use type hints for parameters and return values
  • Enforce scalar, object, array, callable, nullable, and union types
  • Improves debugging, autocomplete, and static analysis
  • Available from PHP 5 (basic) to PHP 8+ (advanced)

Real-World Use Cases:
APIs, form validation, service classes, data transformation, utility libraries.


Frequently Asked Questions (FAQs)

What is a type hint in PHP?
Itโ€™s a way to declare the expected data type for function parameters and return values.

Can I type hint arrays and callables?
Yes, using array and callable as parameter types.

What happens if I pass the wrong type?
PHP throws a TypeError if strict types are enabled.

Whatโ€™s the difference between union and mixed types?
Union specifies exact types (int|string), mixed accepts any type.

How do I enable strict typing in PHP?
Add declare(strict_types=1); at the top of your PHP file.


Share Now :
Share

๐Ÿ“Œ PHP Type Hints

Or Copy Link

CONTENTS
Scroll to Top