新的特性

新增 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 扩展做了多方面的改进,包括:

User Contributed Notes

wallacemaxters at gmail dot com 14-Oct-2016 01:17
After PHP 5.5, foreach with reference in arbitrary array does not generate an error:

    foreach ([1, 2, 3, 4] as $key => &$value) {
        $value = $value . ' R$';
    }

before 5.4, will generate  "Cannot create references to elements of a temporary array expression"
k-gun @ github 22-Mar-2016 06:35
Array dereferencing with defaults for possibly empty sources;

<?php
// string(4) "def2"
$x = ([] ?: ["def1", "def2"])[1];
?>
Anonymous 15-Apr-2015 01:53
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));
        }
    }
}
er dot ankitvishwakarma at gmail dot com 18-Sep-2014 01:48
All the new features are incredible. I had always been waiting for such things to be happened.
mykolas dot sirius at gmail dot com 02-Apr-2014 05:10
Finally, array and string literal dereferencing is here, bless you all!
yannicschne at gmail dot com 01-Apr-2014 08:34
The array and string dereferencing is awesome! Thanks for this.
Love this 24-Jan-2014 03:45
Thanks for making "empty" easier to use and for the array dereferencing!!
emanwebdev at gmail dot com 17-Dec-2013 07:51
Thank You for making empty() to support arbitrary expressions!
PHP Guy 08-Nov-2013 02:59
Great work.  Thank you for allowing Lists to return on foreach.