← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 14 of 24

PHP Type Declarations

A type declaration is like a label on a box saying 'only apples go here'. PHP then complains if you try to put in something else, which catches mistakes early.
Syntax
php
function function_name(type $parameter, ?type $optional): return_type {
    return $value;
}

Scalar Type Declarations

Adding a type before a function parameter (like function greet(string $name)) makes PHP reject calls with the wrong argument type, catching bugs at the call site instead of deep inside the function body.

Example: Scalar Type Declarations

php
<?php
// Define the function `greet` taking `string $name`
function greet(string $name) {
    // Print "Hello, $name!" to the output
    echo "Hello, $name!";
}
// Call `greet("Alice")`
greet("Alice");
?>

Return Type Declarations

A return type declared after the parameter list (function getAge(): int) documents and enforces what a function promises to hand back, which helps both readers and static analysis tools trust the function's contract.

Example: Return Type Declarations

php
<?php
// Define the function `getAge` with no parameters
function getAge(): int {
    // Return `30` from this function
    return 30;
}
// Print `getAge()` to the output
echo getAge();
?>

Nullable Types

Prefixing a type with a question mark (?string) allows either that type or null, which is essential for parameters or return values that genuinely might have no value, like an optional lookup that can fail to find anything.

Example: Nullable Types

php
<?php
// Define the function `findUser` taking `?string $name`
function findUser(?string $name): ?string {
    // Return `$name` from this function
    return $name;
}
// Print a detailed dump (with types) of `findUser(null)`
var_dump(findUser(null));
?>

Strict Types

declare(strict_types=1) at the top of a file disables PHP's usual implicit type coercion for that file, so passing a string where an int is expected throws a TypeError instead of silently converting.

Example: Strict Types

php
<?php
declare(strict_types=1);

function double(int $x): int {
    return $x * 2;
}
echo double(5);
// double("5") would now throw a TypeError instead of silently converting
?>

Iterable Type

The iterable type accepts either a plain array or any object implementing Traversable, letting a function accept 'anything you can foreach over' without caring which concrete form it takes.

Example: Iterable Type

php
<?php
// Define the function `printAll` taking `iterable $items`
function printAll(iterable $items) {
    // Loop over `$items`, binding each item to `$item`
    foreach ($items as $item) {
        // Print `$item . "\n"` to the output
        echo $item . "\n";
    }
}
// Call `printAll([1, 2, 3])`
printAll([1, 2, 3]);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Forgetting declare(strict_types=1); and expecting strict type checks, when PHP otherwise converts types silently.
  2. Returning a value that does not match the declared return type, which throws a TypeError.
  3. Writing a nullable type as int|null and forgetting the short form ?int, or forgetting to handle null inside the function.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.