PHP declare 之 strict_types=1

PHP中申明 declare(strict_types=1)的作用:  strict_types=1 及开启严格模式.默认是弱类型校验.具体严格模式和普通模式的区别见下面代码.

code1:

<?php
declare(strict_types=1);
 
function  foo():int{
    return 1.11;
}
 
echo foo();

code2:

<?php
    //declare(strict_types=1);
     
    function  foo():int{
        return 1.11;
    }
     
    echo foo();

以上代码会怎样呢?

code1 抛出语法错误:

注意:declare 是会校验这个文件下所有使用的的函数,不管他是否是在declare指令文件中申明的!

code2 返回值为1(这里是由于php7新特性决定的);

原文:https://blog.csdn.net/qise4868/article/details/79596119

原文地址:https://www.cnblogs.com/lxwphp/p/9863723.html