- Apr 18, 2013
- by Brad Litwin
PHP is the language of the web. Whatever your personal thoughts on it, its popularity cannot be denied. Many of the web’s most popular applications are written in PHP and even with the growth of Ruby and Node.js, PHP is not going away any time soon. Beyond the massive entrenchment in existing applications, one of the main reasons PHP continues to hold the top spot is that the team behind it continues to add new features. A2 Hosting now offers PHP 5.5 Hosting! Just select PHP 5.5 in your cPanel control panel with our our exclusive PHP Switcher.
PHP 5.5 brings a nice assortment which should make developer’s lives easier. I’ll highlight a few of them here.
Finally, it’s finally!
Those who follow PHP’s development may remember a bit of a kerfuffle about the finally keyword a while ago. It was a highly requested feature that the developers behind PHP insisted wasn’t necessary because its functionality could be duplicated (very clumsily) via other means. Simply put, finally lets you attach code to a try catch block which should be run whether or not an exception is thrown. You might think that means finally is useless since code after the try catch block would run next even without finally, right? In many cases that might be true, but if you have code within your try catch that alters the flow (e.g. throwing another exception which is caught elsewhere) that code after the block may never run or may be run much later. The finally block lets you ensure the code within it always runs before normal execution resumes, which can be quite handy.
Generators via yield keyword
A feature that is coming in ECMAScript 6 as well, Generators are a great design pattern. In PHP (and ES6) they are accomplished via the yield keyword. Yield lets you return a value to the calling code without actually ending execution of the generator function. This means you can iterate over an array of values or objects without actually building up that array. That obviously saves a lot of memory. In asyncronous languages like ES they’re even more useful, but just the saving they provide in PHP is reason enough to use generators over regular iterators where appropriate.
Array and string literal dereferencing
Previously PHP wouldn’t allow you to write [1, 2, 3] you would have to first do $foo = [1, 2, 3] followed by $foo after. While most of the time this means PHP was forcing you to write cleaner code, there can be situations where directly dereferncing a literal is preferable. Well, now you can! Let’s just hope people use this feature wisely.
There’s obviously a lot more in PHP 5.5 than just what I’ve talked about here. Check out the full list for yourself to get it all.