Many of the linux distributions are now adopting PHP 5.4 as their standard version, so it seems like an excellent time to take a look at what will change when you migrate your applications to PHP 5.4. This article covers some of the new features and also gives some tips for migration.
Short Array Syntax
PHP 5.4 introduced a compact array syntax. Rather than using this:
$game = array(’stone‘, ‚paper‘, ’scissors‘);
We can now do this instead:
$game = [’stone‘, ‚paper‘, ’scissors‘];
If you work with JSON notation at any point, then this is probably looking pretty familiar. However, it’s not true JSON! When denoting keys, you’ll still be using PHP’s => operator, so code that used to look like this:
$game = array(0 => ’stone‘, 1 => ‚paper‘, 2 => ’scissors‘);
Now looks like this:
$game = [0 => ’stone‘, 1 => ‚paper‘, 2 => ’scissors‘];
This is a shorter way of creating populated arrays and since nobody likes having to type too much, I think it’s a good addition to the language.
PHP 5.4 Performance Improvements
The short array syntax might not be enough to sell the CTO on the benefits of a PHP upgrade, but this next entry certainly should be! There have been numerous performance improvements in PHP 5.4 when compared to earlier versions of PHP.
A great way to visualise this is to compare how long it takes to run the PHP benchmark.php script (included in the PHP source tree) on different versions of PHP. Here’s what I got when I tried it (full details and the exact timings for each run available at: http://www.lornajane.net/posts/2012/php-5-4-benchmarks)
THIS IS A PREVIEW. DOWNLOAD ISSUE 9 TO READ THE FULL ARTICLE.