PHP - 返回类型声明

PHP - 返回类型声明 首页 / PHP7+入门教程 / PHP - 返回类型声明

在PHP 7中,引入了一个新函数返回类型声明,返回类型声明指定函数应返回的值的类型,可以声明返回类型的以下类型。

  • int
  • float
  • boolean
  • string
  • interfaces
  • array
  • callable

有效返回类型

<?php
   declare(strict_types=1);
   function returnIntValue(int $value): int {
      return $value;
   }
   print(returnIntValue(5));
?>

它产生以下浏览器输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/php7+/php7-returntype-declarations.html

来源:LearnFk无涯教程网

5

无效返回类型

<?php
   declare(strict_types=1);
   function returnIntValue(int $value): int {
      return $value + 1.0;
   }
   print(returnIntValue(5));
?>

它产生以下浏览器输出-

无涯教程网

链接:https://www.learnfk.comhttps://www.learnfk.com/php7+/php7-returntype-declarations.html

来源:LearnFk无涯教程网

Fatal error: Uncaught TypeError: Return value of returnIntValue() must be of the type integer, float returned...

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

深入浅出区块链 -〔陈浩〕

技术管理实战36讲 -〔刘建国〕

后端存储实战课 -〔李玥〕

Service Mesh实战 -〔马若飞〕

说透芯片 -〔邵巍〕

超级访谈:对话汤峥嵘 -〔汤峥嵘〕

超级访谈:对话毕玄 -〔毕玄〕

快速上手C++数据结构与算法 -〔王健伟〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)