PHP Type Declarations
In this page:
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
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
// Define the function `getAge` with no parameters
function getAge(): int {
// Return `30` from this function
return 30;
}
// Print `getAge()` to the output
echo getAge();
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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));
?>
Login to try C/C++/Java/PHP code in the editor
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
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
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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]);
?>
Login to try C/C++/Java/PHP code in the editor
- Forgetting
declare(strict_types=1);and expecting strict type checks, when PHP otherwise converts types silently. - Returning a value that does not match the declared return type, which throws a
TypeError. - Writing a nullable type as
int|nulland forgetting the short form?int, or forgetting to handle null inside the function.
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser