PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
PHP supports type declarations in function signatures. This means you can specify the expected type of parameters and the return type of the function. This helps prevent bugs, makes your code more self-documenting and allows PHP to optimize your code better.
As of PHP 7, the following types can be declared:
bool
int
float
string
array
callable
iterable
object
self
?type
(nullable type)Here is an example of a function with type declarations:
function add(int $a, int $b): int { return $a + $b; }
In this example, $a
and $b
must be integers, and the function must return an integer. If you try to pass a different type or return a different type, PHP will throw a TypeError
.
In addition to these, PHP 7.1 introduced nullable types, which are specified by prefixing the type name with a question mark (?
). Here's an example:
function getFullName(?string $firstName, ?string $lastName): ?string { if ($firstName === null || $lastName === null) { return null; } return $firstName . ' ' . $lastName; }
In this example, $firstName
and $lastName
can either be strings or null
, and the function can either return a string or null
.
Finally, as of PHP 8.0, union types are also supported, allowing a parameter or return value to be of multiple types. For example:
function printData(string|bool|int $data): void { echo $data; }
This means the $data
parameter can be either a string
, bool
, or int
.
Remember, type declarations are a powerful tool that can help you write more robust and understandable code. But also keep in mind that one of the strengths of PHP is its flexibility, so use type declarations where appropriate, but don't feel like you need to use them everywhere if they don't make sense for your application.
Type hinting in PHP function parameters:
function add(int $a, int $b): int { return $a + $b; }
Declaring scalar types (int, float, string, bool) in PHP:
function greet(string $name): string { return "Hello, $name!"; }
Class and interface type declarations in PHP:
class User {} function processUser(User $user): void { // Process User object }
Nullable types and default values in PHP function parameters:
function display(?string $text = null): void { echo $text ?? "No text provided"; }
Using array and iterable types in PHP function declarations:
function processArray(array $data): void { // Process array } function processIterable(iterable $data): void { // Process iterable }
Mixing type declarations and variadic parameters in PHP:
function sum(int ...$numbers): int { return array_sum($numbers); }
Benefits of parameter type declarations for code safety:
function calculatePrice(float $price, int $quantity): float { return $price * $quantity; }
Type coercion and compatibility in PHP function parameters:
function concatStrings(string $str1, string $str2): string { return $str1 . $str2; } concatStrings("Hello", 123); // Error without type coercion