Two years ago, one of our Linux community members discussed some reasons behind the slow adoption to upgrade to PHP 8. One member stated that it’s due to PHP compatibility issues across new releases. In contrast, another put it down to the fact that important software/frameworks did not support PHP 8 until recently. I think there is truth to both those reasons, and that they tie into each other.
PHP 8 was released in November 2020, and as of May 2022, June 2024 November 2024 about 3% (2022) 30% (June 2024) 37% of PHP-based websites currently use PHP 8 as of November 2024! It has taken a lot of software/frameworks a long time to reach full PHP 8 support—for example, WordPress, Magento, MediaWiki, Joomla, and others mentioned by Syndesi. In addition, going forward, Microsoft will not support PHP 8.
PHP 8 has greatly improved in performance and resource usage, so we should upgrade to at least PHP 8.2 for better memory management and faster page loads.
Table of Contents
PHP 8
PHP 8 is a major milestone in the PHP journey, with many new features and improvements for performance, security, and developer experience. Released in November 2020, PHP 8 is designed to fix many of the issues of its predecessors, so it’s a great choice for modern web development.
Despite the advantages, the adoption is slower than expected, only 37% of PHP websites are using it.
Features and Improvements
PHP 8 has many new features. Here are some of them:
- Just-In-Time (JIT) Compilation: One of the most anticipated features, JIT compilation, translates PHP code into machine code at runtime. This results in big performance improvements, especially for complex applications and computationally heavy tasks.
- Attributes: They offer a structured way to specify metadata, replacing the old PHPDoc comments. The code is now cleaner and more maintainable, as metadata is part of the language syntax.
- Union Types: PHP 8 allows more expressive type declarations with union types, no more PHPDoc annotations. This feature improves type safety and makes the code more readable.
- Nullsafe Operator: The nullsafe operator simplifies the process of navigating object properties. It allows method chaining without null checks, less boilerplate code and fewer errors.
- Saner String to Number Comparisons: PHP 8 has more sane string to number comparisons. When comparing a string to a number, PHP will use number comparison and vice versa, so the behavior is more predictable.
- Consistent Type Errors for Internal Functions: Internal functions now throw an Error exception when given invalid parameters. This is a more informative error message, so debugging is faster.
These features and many more make PHP 8 a great tool for developers, performance, better error handling and more expressive code.
Why Are Devs Reluctant to Upgrade to PHP 8?
Perhaps PHP’s lifecycle is too fast for organizations to keep up with? That’s one of the questions that came to mind when I discovered that more than half of all PHP websites are STILL running PHP 7, which loses security support in six months has lost support.
Well, not really. It’s not just a PHP problem. It’s across the board. The enterprise hates to upgrade and wants to stay on the current version as long as possible. This is an issue with many, from Windows 8 to MySQL 8 to RHEL 8. Maybe it’s something about version ‘8’s’? Lol
Companies are hesitant to upgrade because of fear of compatibility issues and service disruptions, so they delay the upgrade instead. When the upgrade becomes critical, that’s when some companies and small business websites start to think about upgrading.
But some just let things run with older, slower and less secure releases as they cling to some security offered by backports.
Take a look at:
PHP 8 Migration Problems
Migrating to PHP 8 is not without problems. The transition is complex, and many issues will arise that need careful planning and execution. Knowing these issues will help developers prepare and mitigate.
Common Problems and Concerns
Here are some of the common problems and concerns when migrating to PHP 8:
- Backward Compatibility: PHP 8 has several backward compatibility breaks that will break existing code. This will require major refactoring to be compatible with the new version.
- Fatal Errors: Changes in language syntax and semantics will cause fatal errors in existing applications. Finding and fixing these errors is crucial for a smooth migration.
- Different PHP Package Versions: Not all PHP packages and dependencies are PHP 8 compatible. Make sure all packages are updated and compatible to avoid conflicts and issues.
- Manual Testing: Automated tests won’t catch all issues, so manual testing is needed to make sure the application works as expected after migration.
- php.ini file: To use PHP 8’s new features and improvements, the php.ini file needs to be updated. This includes enabling new settings and performance tweaks.
- Unit Tests: Changes in language syntax and semantics will break unit tests. Update and run unit tests to catch regressions and ensure code quality.
By knowing these problems and taking a proactive approach to solve them, developers can make the PHP 8 migration smoother and faster.
PHP 8 Upgrade Is Essential for Performance and Security
Source: php.watch/articles/jit-in-depth – PHP 8’s JIT is fast!
Considering a PHP 8 upgrade? Read this: PHP 8 Compatibility Check and Performance Tips.
PHP 7 was at least 2x faster than PHP 5.6. For PHP 7 vs. PHP 8, the improvements are not that drastic but based on many benchmarks; it’s more than enough to be worth it. Besides speed, PHP 8 has new features like the much awaited Just In Time (JIT) compiler (see above benchmark), other performance tweaks and built-in/core JSON support among others. Making sure you’re compatible with newer versions of PHP will allow you to use these latest features and improvements.
PHP 8 Development Best Practices
To use PHP 8 to the fullest, you should follow best practices in writing optimized PHP code. These best practices not only improve performance but also code maintainability and security.
Optimized PHP Code
Here are some best practices for writing optimized PHP code in PHP 8:
- Latest Version: Always use the latest PHP to get the latest security patches and features. Updating PHP is important to keep your application secure and fast.
- PHP Version Manager: Use a PHP version manager like Homebrew or Docker to switch between different PHP versions. This allows you to test and develop with multiple versions and ensure compatibility and performance.
- Code Analyzer: Use a code analyzer like PHPStan or PHP_CodeSniffer to find issues and optimize your code. These tools will help maintain code quality and catch errors early in the development process.
- Unit Tests: Write unit tests to make sure your code works as expected and to catch regressions. Unit tests are crucial for code quality and reliability.
- Migration Guide: Follow a migration guide like the official PHP migration guide to make the transition to PHP 8 smoother. These guides will provide you with step-by-step instructions and best practices to upgrade your codebase.
- Backward Incompatible Changes: Be aware of backward incompatible changes and avoid using deprecated features. This will ensure compatibility with future PHP versions and minimize the need for big refactoring.
- Required Parameters and Type Declarations: Use required parameters and type declarations to make your code robust and maintainable. These will improve type safety and make the code more readable.
- Nullsafe Operator and Union Types: Use the nullsafe operator and union types to write more concise and efficient code. These will reduce boilerplate code and make the code more readable.
By following these best practices, you can optimize your PHP code for PHP 8, security and maintainability.
Conclusion
It’s essential to keep PHP up to date from a security standpoint. Faster PHP performance is significant in addition to security because end-users are becoming less tolerant of slow websites and applications. According to various benchmarks around the web, PHP 8 is considerably faster than PHP 7. In closing, it’s time to upgrade your applications to PHP 8!
So why only 3% of PHP websites have installed or upgraded to PHP 8? Well, it comes down to the culture of the industry. It’s not just a PHP problem.
_____
This is a follow-up article to 78% of the web powered by PHP (3% on PHP 8).
Published: May 10th 2022 | Last updated: November 10th 2024.