Umu*_*vas 1 php casting type-conversion
为什么这段代码可以毫无问题地终止?我认为它会输出一个 TypeError 异常,因为一个 Integer 不能被强制转换或转换为浮点类型的声明 strict_types。
?php
declare(strict_types=1);
function multiply(float $a, float $b): float {
return (double)$a * (double)$b;
}
$six = multiply(2, 3);
echo gettype($six);
//output: double
Run Code Online (Sandbox Code Playgroud)
可以在每个文件的基础上启用严格模式。在严格模式下,只接受类型声明的确切类型的变量,否则
TypeError将抛出 a。此规则的唯一例外是整数可能会被赋予需要浮点数的函数。http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.strict
扩展原始转换不会丢失有关数值整体大小的信息。