Function strict type Arguments

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict
declaration, it will throw a “Fatal Error” if the data type mismatches.
Example:
In the following example we try to send both a number and a string to the function without using strict
:
<?php
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5),
// and it will return 10
?>
To specify strict
we need to set declare(strict_types=1);
. This must be on the very first line of the PHP file.
Example:
In the following example we try to send both a number and a string to the function, but here we have added the strict
declaration:
<?php
declare(strict_types=1); // strict requirement
function addNumbers(int $a, int $b) {
return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is enabled and "5 days" is not an integer,
//an error will be thrown
?>
The
W3Schoolsstrict
declaration forces things to be used in the intended way.
Average Rating