强制模式:
<?php
function
foo(
$a
) : int
{
return
$a
;
}
foo(1.0);
以上代码可以正常执行,foo 函数返回 int 1,没有任何错误。
严格模式:
<?php
declare
(strict_types=1);
function
foo(
$a
) : int
{
return
$a
;
}
foo(1.0);
# PHP Fatal error: Uncaught TypeError: Return value of foo() must be of the type integer, float returned in test.php:6
在声明之后,就会触发致命错误。