小编Sit*_*ose的帖子

类型提示不适用于 php 7 中函数中的字符串

类型提示对于字符串不起作用。

function def_arg(int $name, int $address, string $test){
    return $name . $address . $test;
}

echo def_arg(3, 4, 10) ;
// It doesn't throws an error as expected.
Run Code Online (Sandbox Code Playgroud)

另一方面。如果你在第一个参数中给出字符串,它会抛出一个错误,指出它应该是一个 int。

 function def_arg(int $name, int $address, string $test){
        return $name . $address . $test;
    }

    echo def_arg("any text", 4, "abc") ;

// this code throws an error 
// "Fatal error: Uncaught TypeError: Argument 1 passed to def_arg() must be of the type integer, string given,"
Run Code Online (Sandbox Code Playgroud)

为什么字符串情况下没有错误?

php string types function hints

1
推荐指数
1
解决办法
1215
查看次数

标签 统计

function ×1

hints ×1

php ×1

string ×1

types ×1