©
本文档使用 PHP中文网手册 发布
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 ( 1 , 9 , 2 ) as $number ) {
echo " $number " ;
}
?>
以上例程会输出:
Single digit odd numbers: 1 3 5 7 9
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() 构造将嵌套数组分离到单独的变量。例如:
<?php
$array = [
[ 1 , 2 ],
[ 3 , 4 ],
];
foreach ( $array as list( $a , $b )) {
echo "A: $a ; B: $b \n" ;
}
?>
以上例程会输出:
A: 1; B: 2 A: 3; B: 4
关于 foreach 更深入的文档可参考相关手册页面。
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 literals can now be dereferenced directly to access individual elements and characters:
<?php
echo 'Array dereferencing: ' ;
echo [ 1 , 2 , 3 ][ 0 ];
echo "\n" ;
echo 'String dereferencing: ' ;
echo 'PHP' [ 0 ];
echo "\n" ;
?>
以上例程会输出:
Array dereferencing: 1 String dereferencing: P
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.
The Apache 2.4 handler SAPI is now supported on Windows.
对 GD 扩展做了多方面的改进,包括:
[#1] Anonymous [2015-10-12 10:38:15]
Dereferencing still doesn't work over multiple layers.
Stuff like $function()[0]()[0]() causes syntax errors.
[#2] Anonymous [2015-04-14 16:53:18]
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));
}
}
}
[#3] er dot ankitvishwakarma at gmail dot com [2014-09-17 16:48:43]
All the new features are incredible. I had always been waiting for such things to be happened.
[#4] mykolas dot sirius at gmail dot com [2014-04-02 08:10:32]
Finally, array and string literal dereferencing is here, bless you all!
[#5] yannicschne at gmail dot com [2014-04-01 11:34:52]
The array and string dereferencing is awesome! Thanks for this.
[#6] Love this [2014-01-23 18:45:47]
Thanks for making "empty" easier to use and for the array dereferencing!!
[#7] emanwebdev at gmail dot com [2013-12-17 10:51:07]
Thank You for making empty() to support arbitrary expressions!
[#8] PHP Guy [2013-11-07 17:59:26]
Great work. Thank you for allowing Lists to return on foreach.