PHP 5.5 Stable Now Available

We have been offering PHP 5.5 since April 12th in the form of Beta’s then release candidates but it’s time to say we now offer the stable PHP 5.5 release.  For the vast majority of users PHP 5.5 most likely is not a good idea at this point in time.  A lot of the most common applications have yet to be tested properly under PHP 5.5.  I do know we have some brave users out there already running PHP 5.5 but most likely just for the bragging rights.  He is a quick break down some of the additional features and improvements PHP 5.5 brings for those who have made the switch.

Performance

As usual there are some under the hood changes to the PHP engine.  In this case though the majority of the performance improvements come to the Windows builds which with us being a Linux host we do not run.  The other change being the inclusion of Zend Optimiser+ which is now a bundled extension.  I do have some bad news though right now we do not support this but we’re always looking at ways we can support an Opcode extension for our PHP users.

New Features

In PHP 5.5 I think it’s more about the new features now available.  Here is a quick set of highlights including code examples

Generators
Generators provide a simple interface for iterators .  Here is an example of me finding all the numbers in a range that can evenly be divided by 3.

<?php
function divisible($start, $limit, $divisor)
{
    if ($start < $limit) {
       for ($i = $start; $i <= $limit; $i += 1) {
           if ($i % $divisor == 0) {
              yield $i;
           }
       }
   }
}

$start = 1;
$stop = 10;
$divisor = 3;
echo "Numbers between $start and $stop which can be divided evenly by $divisor are: ";
foreach (divisible($start, $stop, $divisor) as $number) {
   echo "$number ";
}

The final output would be

Numbers between 1 and 10 which can be divided evenly by 3 are: 3 6 9

Finally Keyword
The finally keyword allows you to have code executed within an exception whether it’s caught or not.  Here is an example of some code utilizing finally:

function equalsFive($i)
{
    if ($i != 5) {
        throw new Exception('The number does not equal five');
    }
    echo "$i equals 5";
}
try {
    echo equalsFive(5) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
    echo "First finally.\n";
}
try {
    echo equalsFive(10) . "\n";
} catch (Exception $e) {
    echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
    echo "Second finally.\n";
}

Here is what the output would be with the code above:

5 equals 5
First finally.
Caught exception: The number does not equal five
Second finally.

Password Hashing API
The password hashing API is my favorite new feature in PHP 5.5.  It provides us with an easy to use wrapper around the crypt() function. It’s pretty simple here are only a few functions in this case:


password_get_info
password_hash
password_needs_rehash
password_verify

Here is a quick example of us generating a hash password then verifying that same password

$hash = password_hash('mypassword', PASSWORD_DEFAULT);
if (password_verify('mypassword', $hash)) {
    echo 'Password is valid!';
} else {
    echo 'Invalid password.';
}

This will generate a salted password using bcrypt algorithm.

empty() supports arbitrary expressions
The empty() function now will accept arbitrary expressions.  For example:

function returnFalse() {
    return false;
}

if (empty(returnFalse())) {
    echo "This will be printed.\n";
}

if (empty(true)) {
    echo "This will not be printed.\n";
}

The output of that would be:

This will be printed.

array and string literal dereferencing
PHP 5.5 now supports array and string literal dereferencing and here’s a quick example of that:

<?php
echo 'Array dereferencing: ';
echo [1, 2, 3][0];
echo "\n";

echo 'String dereferencing: ';
echo 'HawkHost'[0];
echo "\n";

The output of the above code would be:

Array dereferencing: 5
String dereferencing: H

There are a few other features that I won’t go into but they can all be found at the php.net new features page.  I’m sure not too many are going to be brave and switch to PHP 5.5 as of this moment but if you are php version selector will make it a quick switch.  Now the question is, will your code work in PHP 5.5? Well, that’s the great thing about the selector! If it doesn’t you can quickly switch back to PHP 5.2, 5.3 or 5.4.

This entry was posted in General and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *