新的特性

新增 Generators

Support for generators has been added via the yield keyword. Generators provide an easy way to implement simple iterators without the overhead or complexity of implementing a class that implements the Iterator interface.

A simple example that reimplements the range() function as a generator (at least for positive step values):

<?php
function xrange($start$limit$step 1) {
    for (
$i $start$i <= $limit$i += $step) {
        
yield $i;
    }
}

echo 
'Single digit odd numbers: ';

/* 注意保存在内存中的数组绝不会被创建或返回 */ 
foreach (xrange(192) as $number) {
    echo 
"$number ";
}
?>

以上例程会输出:

Single digit odd numbers: 1 3 5 7 9 

新增 finally 关键字

try-catch blocks now support a finally block for code that should be run regardless of whether an exception has been thrown or not.

foreach 现在支持 list()

foreach 控制结构现在支持通过 list() 构造将嵌套数组分离到单独的变量。例如:

<?php
$array 
= [
    [
12],
    [
34],
];

foreach (
$array as list($a$b)) {
    echo 
"A: $a; B: $b\n";
}
?>

以上例程会输出:

A: 1; B: 2
A: 3; B: 4

关于 foreach 更深入的文档可参考相关手册页面。

empty() 支持任意表达式

empty() 现在支持传入一个任意表达式,而不仅是一个变量。例如:

<?php
function always_false() {
    return 
false;
}

if (empty(
always_false())) {
    echo 
'This will be printed.';
}

if (empty(
true)) {
    echo 
'This will not be printed.';
}
?>

以上例程会输出:

This will be printed.

array and string literal dereferencing

Array and string literals can now be dereferenced directly to access individual elements and characters:

<?php
echo 'Array dereferencing: ';
echo [
123][0];
echo 
"\n";

echo 
'String dereferencing: ';
echo 
'PHP'[0];
echo 
"\n";
?>

以上例程会输出:

Array dereferencing: 1
String dereferencing: P

新的密码哈希 API

A 新的密码哈希 API that makes it easier to securely hash and manage passwords using the same underlying library as crypt() in PHP has been added. See the documentation for password_hash() for more detail.

Apache 2.4 handler supported on Windows

The Apache 2.4 handler SAPI is now supported on Windows.

改进 GD

对 GD 扩展做了多方面的改进,包括:

add a note add a note

User Contributed Notes 7 notes

up
102
emanwebdev at gmail dot com
1 year ago
Thank You for making empty() to support arbitrary expressions!
up
3
Anonymous
3 months ago
Yield is awesome... it's starts to look like a real language now ;-)

class myList {
    public $list;
    public function __construct($list) {
        $this->list = $list;
    }

    public function select(...$keys) {
        $keys_array = array_fill_keys($keys, null);
        $items = $this->list;
        foreach($items as $item) {
            yield array_merge($keys_array, array_intersect_key($item, $keys_array));
        }
    }
}
up
39
yannicschne at gmail dot com
1 year ago
The array and string dereferencing is awesome! Thanks for this.
up
10
er dot ankitvishwakarma at gmail dot com
10 months ago
All the new features are incredible. I had always been waiting for such things to be happened.
up
12
mykolas dot sirius at gmail dot com
1 year ago
Finally, array and string literal dereferencing is here, bless you all!
up
13
Love this
1 year ago
Thanks for making "empty" easier to use and for the array dereferencing!!
up
11
PHP Guy
1 year ago
Great work.  Thank you for allowing Lists to return on foreach.
To Top