总结PHP 7.x 各个版本的新特性

广告:宝塔服务器面板,一键全能部署及管理,送你10850元礼包,点我领取~~~

总结PHP 7.x 各个版本的新特性

推荐(免费):PHP7

PHP 7.x 各个版本的新特性

前言

上个月同事看见我写

$a = $a ?? '';
登录后复制

问我这个写法是什么,还有这样的写法?我说这是PHP7以上才有的写法,你不知道吗?他说不知道。

心里嘀咕了一下,打算开始写这篇博客。

PHP7 应该是除了基础之外,是一种现在的 PHP 。因为在PHP7 出现了,强类型定义,和一些语法上的写法,如 组合比较符, define() 可以定义数组等一些特性。下面开始正式介绍,从PHP7.0 开始介绍,以后出了新版本,也会在下面陆陆续续加上。 好了,我们开始

PHP 7.0

标量类型声明

什么是标量类型?

四种标量类型: boolean (布尔型) integer (整型) float (浮点型, 也称作 double) string (字符串) 两种复合类型: array (数组) object (对象) 资源是一种特殊变量,保存了到外部资源的一个引用。资源是通过专门的函数来建立和使用的。资源类型变量为打开文件、数据库连接、图形画布区域等的特殊句柄。 说的通俗一点,标量类型,就是定义变量的一个数据类型。

在php5中,有类名,接口,数组 和回调函数。在php中,增加了 符串(string), 整数 (int), 浮点数 (float), 以及布尔值 (bool)。下面我们来举例子,万事万物看例子

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given
登录后复制

在这里,我们定义了$a 必须为int类型,如果 type 函数里面传了string 所以报错。让我们修改上述的代码就没错了

function typeString(string $a){    echo $a;}typeString('sad'); //sad
登录后复制

返回值类型声明

关于函数的方法返回值可以定义,比如我某个函数必须要返回 int 类型,他就定死来返回 int ,如果你返回 string 则报错。如下

<?phpfunction returnArray(): array{    return [1, 2, 3, 4];}print_r(returnArray());/*Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4)*/
登录后复制

那当我们的定义了数组,返回了string或者其他类型呢?

那么他将会报错 比如

function returnErrorArray(): array{    return '1456546';}print_r(returnErrorArray());/*ArrayFatal error: Uncaught TypeError: Return value of returnArray() must be of the type array, string returned in */
登录后复制

null 合并运算符

由于日常使用中存在大量同时使用三元表达式和 isset()的情况, 我们添加了null合并运算符 (??) 这个语法糖。如果变量存在且值不为NULL, 它就会返回自身的值,否则返回它的第二个操作数。

<?php$username = $_GET['user'] ?? 'nobody';//这两个是等效的  当不存在user 则返回?? 后面的参数$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';?>
登录后复制

太空船操作符

// 整数echo 1 <=> 1; // 0 当左边等于右边的时候,返回0echo 1 <=> 2; // -1  当左边小于右边,返回-1echo 2 <=> 1; // 1  当左边大于右边,返回1// 浮点数echo 1.5 <=> 1.5; // 0echo 1.5 <=> 2.5; // -1echo 2.5 <=> 1.5; // 1 // 字符串echo "a" <=> "a"; // 0echo "a" <=> "b"; // -1echo "b" <=> "a"; // 1
登录后复制

define 定义数组

在PHP7 以前的版本 define 是不能够定义数组的 现在是可以的 比如

define('ANIMALS', [    'dog',    'cat',    'bird']);echo ANIMALS[1]; // 输出 "cat"
登录后复制

use 方法批量导入

// PHP 7 之前的代码use some\namespace\ClassA;use some\namespace\ClassB;use some\namespace\ClassC as C;use function some\namespace\fn_a;use function some\namespace\fn_b;use function some\namespace\fn_c;use const some\namespace\ConstA;use const some\namespace\ConstB;use const some\namespace\ConstC;// PHP 7+ 及更高版本的代码use some\namespace\{ClassA, ClassB, ClassC as C};use function some\namespace\{fn_a, fn_b, fn_c};use const some\namespace\{ConstA, ConstB, ConstC};
登录后复制

###Unicode codepoint 转译语法

echo "\u{aa}"; //ªecho "\u{0000aa}";  //ª  echo "\u{9999}"; //香
登录后复制

匿名类

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given0
登录后复制

PHP 7.1

可为空类型

参数以及返回值的类型现在可以通过在类型前加上一个问号使之允许为空。 当启用这个特性时,传入的参数或者函数返回的结果要么是给定的类型,要么是 null 。

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given1
登录后复制

void

增加了一个返回void的类型,比如

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given2
登录后复制

多异常捕获处理

这个功能还是比较常用的,在日常开发之中

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given3
登录后复制

PHP 7.2

PHP7.2是PHP7系列 最少的新特性了

###允许分组命名空间的尾部逗号 比如

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given4
登录后复制

允许重写抽象方法

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given5
登录后复制

新的对象类型

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given6
登录后复制

PHP 7.3

PHP7.3 在语法层面没有什么很大的改变。

PHP 7.4

类属性支持类型声明

恭喜 PHP 往强类型又迈了一步

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given7
登录后复制

箭头函数

箭头函数提供了用于使用隐式按值作用域绑定定义函数的简写语法。与 JS 的箭头函数差不多,不过要带个 fn。吐槽一波

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given8
登录后复制

Null 合并运算符支持方法

function typeInt(int $a){    echo $a;}typeInt('sad');// 运行,他讲会报错 Fatal error: Uncaught TypeError: Argument 1 passed to type() must be of the type integer, string given9
登录后复制

以上就是总结PHP 7.x 各个版本的新特性的详细内容,更多请关注9543建站博客其它相关文章!

9543建站博客
一个专注于网站开发、微信开发的技术类纯净博客。
作者头像
admin创始人

肥猫,知名SEO博客站长,14年SEO经验。

上一篇:前端与web前端的区别
下一篇:怎么解决laravel excel乱码问题

发表评论

关闭广告
关闭广告