Skip to content
PHP Logo

How does PHP 7 enforce scalar types via Typehinting?

PHP 7 enforces scalar types via Typehinting by allowing developers to specify the data type of function parameters and return values. This helps ensure that only the specified data types are accepted, preventing unexpected type conversions or errors in the code.

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

echo add(5, 10); // Output: 15
echo add("5", 10); // Error: Argument 1 passed to add() must be of the type int, string given