list() support by foreach statement:
It introduces list () support for foreach statement. While working with nested arrays, you can unpack the array elements into variables in each iteration via list().
$product = [
["Nokia", "Lumia"],
["Samsung", "Galaxy"],
["Apple", "iPod"],
];
foreach ($product as list($Brand, $device)) {
echo "Brand: $Brand; Device: $device\n";
}
Output:
Brand: Nokia; Device: Lumia
Brand: Samsung; Device: Galaxy
Brand: Apple; Device: iPod
Generator Support
Generator provides an alternative way to implement iterator without implementing the iterator interface and thus remove the overhead of iterator. Generator is very similar to a function, difference is instead of returning value, it yields its value. The keyword yield is used for this purpose.
<?php
function generatorTest() {
echo "Generator starts\n";
for ($count = 1; $count < 6; $count++ ) {
yield $count;
echo "count: $count \n";
}
echo "Generator ends";
}
foreach (generatorTest() as $gen);
?>
Output:
Generator starts
count: 1
count: 2
count: 3
count: 4
count: 5
Generator ends
Array and string dereferencing support:
PHP 5.5 adds support for array dereferencing into individual element as well as string dereferencing into individual characters.
<?php
echo 'Array dereferencing example:';
echo [0, 1, 2][0];
echo "\n";
echo 'String dereferencing example:';
echo 'Web'[0];
echo "\n";
?>
Output:
Array dereferencing example: 0
String dereferencing example: W
Finally keyword for exception handling:
In php 5.5, finally keyword has been introduced for exception handling. In previous versions, exceptions were handled by try catch block only, no finally block was available. The finally keyword is followed by a block of code, which is placed after every catch block. This block of code will always be executed, however any exception is thrown or not.
function division($x,$y) {
if ($y==0)
{
throw new Exception('Divide by zero');
}
return $x/$y;
}
try {
echo division(10,0) . "\n";
} catch (Exception $ex) {
echo 'Caught exception: ', $ex->getMessage(), "\n";
} finally {
echo "Finally block after division.\n";
}
empty() function support for expression:
In previous versions, empty() function was able to only check value of a variable, by taking a variable as an argument. Now any expression can be used as argument in empty function to perform the same stuff.
<?php
$ a = 5;
$b = 8;
function check_val() {
if(($a - $b) > 0)
return true;
else
return false;
}
if (empty(check_val ())) {
echo "False returned.\n";
}
else
echo "True returned.\n";
?>
Output:
True returned.
New password API:
This version introduces a secure password hashing API, which is one of the most important contributions. Previously, we had to depend on crypt() function and md5 technology which lacks security and is somewhat confusing. In new API, hashing is performed by bcrypt() function, which returns a strong hash with automatically salted. PHP 5.5 introduces two new function named password_hash() and password_verify().
New features in GD library
PHP GD extension has improved its ability with some advanced image operations. Here is a list of new functions for image different operation:
- Imageflip() : Provides image flipping support
- Imagecrop() and imagecropauto() : Provides advanced image cropping support
- Imagecreatefromwebp() and imagewebp() : WebP read and write support
The introduction of new features in PHP 5.5 has contributed greatly to upgrade the language and PHP development.
Read also introduction to HTML5.