在PHP 7中,引入了一个新函数,即标量类型声明。标量类型声明有两个选项-
Coercive - 强制性是默认模式。
Strict - 严格模式必须明确提示。
可以使用上述模式强制执行以下类型的函数参数-
链接:https://www.learnfk.comhttps://www.learnfk.com/php7+/php7-scalartype-declarations.html
来源:LearnFk无涯教程网
<?php //Coercive mode function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); ?>
它产生以下浏览器输出-
9
<?php //Strict mode declare(strict_types=1); function sum(int ...$ints) { return array_sum($ints); } print(sum(2, '3', 4.1)); ?>
它产生以下浏览器输出-
Fatal error: Uncaught TypeError: Argument 2 passed to sum() must be of the type integer, string given, ...
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
Spring Cloud 微服务项目实战 -〔姚秋辰(姚半仙)〕