5.5.15

New features

Generators added

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: ';

/* Note that an array is never created or returned,
 * which saves memory. */ 
foreach (xrange(192) as $number) {
    echo 
"$number ";
}
?>

Powyższy przykład wyświetli:

Single digit odd numbers: 1 3 5 7 9 

finally keyword added

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 now supports list()

The foreach control structure now supports unpacking nested arrays into separate variables via the list() construct. For example:

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

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

Powyższy przykład wyświetli:

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

Further documentation is available on the foreach manual page.

empty() supports arbitrary expressions

Passing an arbitrary expression instead of a variable to empty() is now supported. For example:

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

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

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

Powyższy przykład wyświetli:

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";
?>

Powyższy przykład wyświetli:

Array dereferencing: 1
String dereferencing: P

New password hashing API

A new password hashing 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.

add a note add a note

User Contributed Notes 6 notes

up
48
emanwebdev at gmail dot com
7 months ago
Thank You for making empty() to support arbitrary expressions!
up
13
yannicschne at gmail dot com
3 months ago
The array and string dereferencing is awesome! Thanks for this.
up
13
ahmad dot samiei at gmail dot com
5 months ago
Thank you to make PHP even better than before.
up
3
mykolas dot sirius at gmail dot com
3 months ago
Finally, array and string literal dereferencing is here, bless you all!
up
5
Love this
6 months ago
Thanks for making "empty" easier to use and for the array dereferencing!!
up
7
PHP Guy
8 months ago
Great work.  Thank you for allowing Lists to return on foreach.
To Top