What Is New in PHP 5.5
PHP 5.5 delivers a significant upgrade with new language features, performance improvements, and the removal of legacy functionality. This release focuses on modernizing the language for more expressive and efficient code.
| Category | Key Changes |
|---|---|
| New Features | Generators, finally keyword, constant array/string dereferencing, simplified password hashing API |
| Performance | OPcache extension now bundled and compiled by default |
| Deprecated | Windows XP and 2003 support, mysql extension, preg_replace /e modifier |
| Changes | SSL improvements, support for MySQLnd as default, updated Ext/Intl |
What are the major new language features?
Generators are the standout feature, introducing a simple way to implement iterators without the overhead of a full class. You use the yield keyword to provide values to a foreach loop on the fly.
function numberGenerator() {
for ($i = 0; $i < 5; $i++) {
yield $i;
}
}
foreach (numberGenerator() as $number) {
echo $number;
}
The finally block brings PHP's exception handling in line with other languages, guaranteeing that a block of code will run after a try, regardless of whether an exception was thrown or caught.
How did password handling improve?
PHP 5.5 introduced a dedicated, simple, and secure API for password hashing. The new functions password_hash() and password_verify() abstract away the complexity of salting and using strong algorithms like BCrypt.
// Hashing a password
$hash = password_hash('my_password', PASSWORD_DEFAULT);
// Verifying a password
if (password_verify('my_password', $hash)) {
// Password is valid
}
This was a huge win for security, making it much harder for developers to incorrectly implement password storage. The old methods involving md5() or sha1() became instantly obsolete.
What performance enhancements were made?
The most significant performance boost came from bundling the Zend OPcache extension directly into the core and enabling it by default. OPcache stores precompiled script bytecode in shared memory, eliminating the need for PHP to load and parse scripts on every request.
This change drastically reduced CPU usage and improved throughput for most applications. For many, simply upgrading to 5.5 provided a free speed upgrade without any code changes.
What was deprecated and removed?
This release continued the push to clean up old cruft. The original mysql extension was officially deprecated, urging developers to move to mysqli or PDO. The preg_replace /e modifier was also deprecated due to its security implications.
Support for older Windows operating systems (XP and 2003) was dropped, allowing the core team to focus on modern platforms. This cleanup is a necessary part of keeping the language modern and maintainable.
FAQ
Do I have to use OPcache in PHP 5.5?
It is compiled and enabled by default, but you can configure it or disable it in your php.ini file. For production use, leaving it on is highly recommended for the performance benefits.
Why should I stop using the mysql_* functions?
The mysql extension is deprecated in 5.5 and was completely removed in a later version (PHP 7.0). It lacks support for modern MySQL features, prepared statements, and stored procedures offered by mysqli and PDO.
What is the advantage of using generators?
Generators are memory efficient. They allow you to iterate over large data sets or compute-intensive operations without needing to build and store a large array in memory first, which is great for processing large files or database results.
Is the new password API more secure?
Yes, absolutely. It handles secure salt generation automatically and uses a strong hashing algorithm (BCrypt by default). It effectively prevents common mistakes developers made when trying to implement hashing manually.
What does constant dereferencing allow me to do?
It allows you to directly access an element of an array or a character of a string that is returned by a function or a constant, without needing to use a temporary variable. For example: echo getArray()[0]; or echo 'ABCD'[1]; // outputs 'B'