Junior — Middle
Can PHP use data types, and how is this implemented?
sobes.tech AI
Answer from AI
Yes, in PHP you can use data types, starting from version 7.0 support for strict typing and type declarations for function parameters and return values was introduced.
Data types can be specified for function parameters, return values, and from PHP 7.4 — for class properties.
Example of using type declarations in PHP:
function sum(int $a, int $b): int {
return $a + $b;
}
$result = sum(3, 5); // 8
You can also enable strict typing in a file by adding at the beginning:
declare(strict_types=1);
This will force PHP to strictly check types, otherwise an error will be thrown.