Tweet
submit to reddit
<? mildcoder ?>
Home | About Me | Contact Us
Categories
  • Security
  • Research
  • Classes
  • Downloads
  • Tutorials
  • News
Search
Advertisements

What is new in PHP 5.5.0

The latest version of PHP, PHP 5.5.0, is out with lots of new features, fixes and functional enhancements. Most of the changes will not make much disturbance on existing application, but still you will have to do a through testing when you migrate to PHP 5.5.0 version. Here are 10 such notable improvements that may prove quite useful for web developers.

Range generators now make iterations easier

Developers no longer have to implement an Iterator interface to iterate through data, thanks to Generators available with this new release. A generator basically resembles a function and is defined using the function keyword, but is different from a function as it can return multiple results with the help of the yield keyword.

Here’s an example of how a generator can iteratively print a sequence of integers.

1
2
3
4
5
6
7
8
9
10
11
12
13
function xrange($start, $end, $step = 1) {
    for ($i = $start; $i <= $end; $i += $step) {
        yield $i;
    }
}

echo 'Single digit odd numbers: ';

foreach (xrange(1, 9, 2) as $number) {
    echo "$number ";
}

echo "\n";

The generator function output will be: “Single digit odd numbers:1,3,5,7,9″.

Notice that the generator mimics the behavior of an array as it iterates through the data using a foreach loop, without actually using memory space to build the array, saving precious processing time and memory in the process.

Consider the following example of using the range() function:

1
2
3
4
5
foreach (range(0,1000000) as $number) {
    echo "$number ";
}

echo "\n";

According to the PHP Manual, this bit of code will use over 100MB to generate one million sequential numbers by building an array to store these values, while the using a generator function to create the sequence may just use about 1KB to complete the task.

Finally keyword added to improve exception handling

The addition of the “finally” keyword refines the way that PHP deals with Exception handling in PHP is no different from other high level languages. Code is usually wrapped in a try and catch block, with exceptions being detected in the try block and handled in the catch block.

The “finally” keyword in way completes exception handling, allowing developers to put a block of code after the try and catch block so that it gets executed by default, irrespective of the exception.

Here’s an example from the PHP manual:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function inverse($x) {
    if (!$x) {
        throw new Exception('Division by zero.');
    }
    return 1/$x;
}

try {
    echo inverse(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: '$e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}

“First finally” will be printed regardless of the exception, which, if detected, will throw up its own exception message as well.

New password hashing API

Developers can now create a salted password hash with just a single of code by using the new password hashing API and bcrypt, the default algorithm for encryption.

Sample Code:

1
$hash = password_hash($password, PASSWORD_DEFAULT);

The password_hash() function accepts two arguments, the first one being the password in string format and the second, a constant indicating the encryption algorithm to be applied.

The salted password can be double checked using the password_verify() function in indicated in the following code:

1
password_verify($password, $hash);

Make sure to save the salted password in a database column that can hold over 60 characters to avoid any data truncation.

Future PHP releases are however expected to strengthen password encryption with newer algorithms which may eventually replace bcrypt.

Array and string literal deferencing added

Developers can now directly deference and access individual array elements or characters from array and string literals.
For Example:

1
2
3
echo 'Array dereferencing:';
echo [4,5,6][0];
echo "\n";

This piece of code selects the first array element and prints it on screen, resulting in “Array dereferencing:4″ as output.

1
2
3
echo 'String dereferencing:';
echo 'HELLO'[1];
echo "\n";

This code selects the second element in the string to display or print “String dereferencing:E”.

Easier class name resolution

PHP 5.5.0 now makes it easier to resolve class names as the class keyword not only retrieves the fully qualified class name but also the namespace where the class is defined.

For Example:

1
2
3
4
5
namespace NS {
    class ClassName {
    }
    echo ClassName::class;
}

Output: NS\ClassName

Empty() function now accepts expressions

The empty() function, commonly used to determine return value of variables, now accepts expressions as arguments and can be used to determine if expressions return empty or false values.

For example, executing the following piece of code will display “False value returned” on the screen.

1
2
3
4
5
6
7
8
9
function send_false() {
    return false;
}
if (empty(send_false())) {
    echo "False value returned.\n";
}
if (empty(true)) {
    echo "True.\n";
}

Support for the Zend Optimiser+ opcode cache added

The new OPcache extension now houses the Zend Optimiser+ opcode cache in PHP.

Performance improvement is the main advantage of the OPcache, which stores the precompiled PHP script bytecode in shared memory, doing away with the need for PHP to reload and parse scripts for each repeated request.

foreach loops now support the list() construct

It is now simple to read through nested arrays using a foreach() loop as the values stored in the array elements can directly be assigned to variables in a list() construct.

List() construct now supports variables that can hold array element values. For example,

1
2
3
4
5
6
7
8
// Create an array 

$animals = array('dog', 'fox');

// Listing all the values from the array using variables to form this familiar phrase, “The quick fox jumped over the lazy dog”.
list($animal1, $animal2) = $animals;

echo "The quick $animal1 jumped over the lazy $animal2\n";

It is now possible to use the list() construct with the foreach() loop to assign nested array element values to variables.

Here’s an example:

1
2
3
4
5
6
7
$array = [
    [10, 20],
    [30, 40],
];
foreach ($array as list($a, $b)) {
    echo "First: $a; Second: $b\n";
}

Output:

First: 10, Second: 20
First: 30, Second: 40

New features added to GD library

PHP’s GD extension has been considerably enhanced to include new features to handle image creation and manipulation. Here are few new functions added to the library:

Function(s) Purpose
imageflip() Flipping support
imagecrop() Advanced cropping support
imagecropauto()
imagecreatefromwebp() WebP read and write support
imagewebp()

foreach loops now support non-scalar keys

The foreach loop can now iterate through arrays that have non-scalar element keys (i.e. values other than string or integer).

Deprecations

However, the main highlight (or drawback) is that this version is not compatible with Windows XP and 2003. Do check here for other changes that may affect backward compatibility before going ahead with the upgrade. Following are the deprecations in PHP 5.5.0:

  • No more support on Windows XP and Windows Server 2003 OS
  • Should use MySQLi or PDO_MySQL instead of old MySQL extension
  • preg_replace /e is deprecated
  • Logo GUIDs is deprecated
  • IntlDateFormatter::setTimeZoneID() and datefmt_set_timezone_id() are deprecated, instead can go with IntlDateFormatter::setTimeZone() and datefmt_set_timezone()
  • mcrypt_cbc(),mcrypt_cfb(),mcrypt_ecb(),mcrypt_ofb() are deprecated