1. ?? 运算符(NULL 合并运算符)
把这个放在第一个说是因为我觉得它很有用。用法:
$a = $_GET['a'] ?? 1;
它相当于:
<?phpif (empty($_GET['a'])) $a = 1;
我们知道三元运算符是可以这样用的:
$a ?: 1
但是这是建立在 $a 已经定义了的前提上。新增的 ?? 运算符可以简化判断。
2. 函数返回值类型声明
官方文档提供的例子(注意 ... 的边长参数语法在 PHP 5.6 以上的版本中才有):
<?phpfunction arraysSum(array ...$arrays): array{ return array_map(function(array $array): int { return array_sum($array); }, $arrays);}print_r(arraysSum([1,2,3], [4,5,6], [7,8,9]));
从这个例子中可以看出现在函数(包括匿名函数)都可以指定返回值的类型。
这种声明的写法有些类似于 swift:
func sayHello(personName: String) -> String { let greeting = "Hello, " + personName + "!" return greeting}
这个特性可以帮助我们避免一些 PHP 的隐式类型转换带来的问题。在定义一个函数之前就想好预期的结果可以避免一些不必要的错误。
不过这里也有一个特点需要注意。PHP 7 增加了一个 declare 指令:strict_types,既使用严格模式。
使用返回值类型声明时,如果没有声明为严格模式,如果返回值不是预期的类型,PHP 还是会对其进行强制类型转换。但是如果是严格模式, 则会出发一个 TypeError 的 Fatal error。
强制模式:
<?phpfunction foo($a) : int{ return $a;}foo(1.0);
以上代码可以正常执行,foo 函数返回 int 1,没有任何错误。
严格模式:
<?phpdeclare(strict_types=1);function foo($a) : int{ return $a;}foo(1.0);# PHP Fatal error: Uncaught TypeError: Return value of foo() must be of the type integer, float returned in test.php:6
在声明之后,就会触发致命错误。
是不是有点类似与 js 的 strict mode?
3. 标量类型声明
PHP 7 中的函数的形参类型声明可以是标量了。在 PHP 5 中只能是类名、接口、array 或者 callable (PHP 5.4,即可以是函数,包括匿名函数),现在也可以使用 string、int、float和 bool 了。
官方示例:
<?php// Coercive modefunction sumOfInts(int ...$ints){ return array_sum($ints);}var_dump(sumOfInts(2, '3', 4.1));
需要注意的是上文提到的严格模式的问题在这里同样适用:强制模式(默认,既强制类型转换)下还是会对不符合预期的参数进行强制类型转换,严格模式下则触发 TypeError 的致命错误。
4. use 批量声明
PHP 7 中 use 可以在一句话中声明多个类或函数或 const 了:
<?phpuse some\namespace\{ClassA, ClassB, ClassC as C};use function some\namespace\{fn_a, fn_b, fn_c};use const some\namespace\{ConstA, ConstB, ConstC};
但还是要写出每个类或函数或 const 的名称(并没有像 python 一样的 from some import * 的方法)。
需要留意的问题是:如果你使用的是基于 composer 和 PSR-4 的框架,这种写法是否能成功的加载类文件?
5. 其他的特性
其他的一些特性我就不一一介绍了,有兴趣可以查看官方文档:
http://php.net/manual/en/migration70.new-features.php
简要说几个:
PHP 5.3 开始有了匿名函数,现在又有了匿名类了;
define 现在可以定义常量数组;
闭包( Closure)增加了一个 call 方法;
生成器(或者叫迭代器更合适)可以有一个最终返回值(return),也可以通过 yield from 的新语法进入一个另外一个生成器中(生成器委托)。
生成器的两个新特性(return 和 yield from)可以组合。具体的表象大家可以自行测试。PHP 7 现在已经到 RC5 了,最终的版本应该会很快到来。
最后打个小广告:
5pyJ5oSP57uE5bu65LiA5Liq5LiN5rC055qEUEhQ55qEUVHnvqTvvJozMDU2MTEwODjvvIzmrKLov47liqDlhaXjgII=

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools