Scalar type declaration is either coercive (no need to be specified explicitly) or strict (type hinted strictly). By default, types are coercive.
Coercive means that PHP will coerce a number to an integer even if it's a string.
Take the following example. Here, we have a function called number that's been type hinted to only accept integers.
In this example, a string is being passed. When running PHP in coercive mode (this is on by default), this will work and print 1
to the screen:
<?php
function number(int $int)
{
echo "the number is: $int";
}
number('1');
To facilitate strict mode, a single declare
directive is placed at the top of the file containing the following:
declare(strict_types=1);
Now, run the example again in strict mode:
<?php
declare(strict_types=1);
function number(int $int)
{
echo "the number is: $int";
}
number('1');
This...