Php Scalar Return Type
## Introduction
In PHP, type safety and code predictability have evolved significantly over the years. One of the most impactful features introduced in PHP 7.0 is **Scalar Return Type Declarations**.
Before PHP 7, functions could return any data type without restriction, often leading to runtime bugs, unexpected behaviors, and the need for extensive boilerplate validation code. With scalar return types, you can explicitly declare the expected data type of a function's return value. This ensures that your functions adhere to a strict contract, making your codebase more robust, self-documenting, and easier to debug.
---
## Syntax and Usage
To declare a return type, append a colon (`:`) followed by the desired type after the function's parameter list.
### Basic Syntax
```php
function functionName(parameter_list): return_type {
// Function body
return value;
}
```
### Supported Scalar Types
PHP supports the following scalar types for return declarations:
| Type | Description |
| :--- | :--- |
| `string` | The function must return a sequence of characters. |
| `int` | The function must return an integer. |
| `float` | The function must return a floating-point number. |
| `bool` | The function must return a boolean value (`true` or `false`). |
### Coercive Mode vs. Strict Mode
PHP operates in two type-checking modes, which drastically affect how scalar return types are handled:
1. **Coercive Mode (Default):** PHP will attempt to automatically cast (coerce) the returned value to the declared type if possible. For example, if a function declares an `int` return type but returns the string `"42"`, PHP will silently convert it to the integer `42`.
2. **Strict Mode:** When strict types are enabled, PHP will not perform any automatic type casting. If the returned value does not exactly match the declared type, PHP will throw a `TypeError`. Strict mode is enabled on a per-file basis by adding `declare(strict_types=1);` at the very top of the PHP file.
---
## Code Examples
### 1. Coercive Mode (Default Behavior)
In this example, strict types are not enabled. PHP automatically coerces the float value to an integer.
```php
```
### 2. Strict Mode (Recommended)
By declaring `strict_types=1`, PHP enforces exact type matching. If a function returns a type that does not match the declaration, a `TypeError` is thrown.
```php
getMessage();
// Output: Error: Return value of getAgeString() must be of type string, int returned
}
?>
```
### 3. Nullable Return Types (PHP 7.1+)
Sometimes a function might return a scalar value or `null`. You can make a return type nullable by prefixing it with a question mark (`?`).
```php
"Alexander",
2 => null // User has no middle name
];
return $database[$userId] ?? null;
}
var_dump(getUserMiddleName(1)); // Output: string(9) "Alexander"
var_dump(getUserMiddleName(2)); // Output: NULL
```
### 4. Union Return Types (PHP 8.0+)
If a function can return multiple different scalar types, you can use Union Types by separating them with a pipe character (`|`).
```php
YouTip