What is the best PHP version for WordPress?
We always want to choose the latest PHP version for WordPress. But before deciding which PHP version you are going to use, you really should consider the most critical part, which is knowing which PHP version is compatible with your version of WordPress.
Assuming you are on the latest version of WordPress, which is currently at 6.2. According to the WordPress documentation, PHP 7.4 is currently recommended for WordPress. Your website can run on all PHP versions, but there may be some unexpected behaviors with compatibility.
It’s important to know that WordPress still considers PHP 8.0, PHP 8.1, and PHP 8.2 under “Beta Support,” meaning they may not be as stable as PHP 7.4 and may not be compatible with certain WordPress plugins and themes.
Compatibility with WordPress Themes and Plugins
Even though the plugin developers update their plugins to the latest PHP versions, it is important to remember that they still rely on the WordPress core. So when Themes or 3rd-party plugins notify us that they are compatible with the latest PHP version, I still go by the compatibility of the WordPress core. Also, remember that not all plugins will use the same WordPress php version as others. It would become tedious to check each plugin frequently to see which is compatible with the PHP Version you want to set for your website.
Now all of this said, I want to talk about the improvements of the latest PHP versions, especially regarding dynamic content performance. But most importantly, the improvements of the latest PHP versions will help with compatibility for future PHP upgrades by introducing things such as Union Types and Stricter Type Checks which can help themes and plugins be more compatible with different PHP versions.
What’s New in PHP 8.0?
The (JIT) compiler
The JIT compiler dynamically translates PHP code into a more optimized format at runtime, allowing faster execution.
- When processing complex queries, rendering templates, or executing resource-intensive tasks, the JIT compiler can boost performance and responsiveness.
- Complex functionalities, such as advanced form builders, image galleries, or e-commerce plugins, can benefit from the optimized execution of the JIT compiler.
- The JIT compiler introduced in PHP 8.0 can improve scalability by reducing the processing time for resource-intensive operations. This means your website can handle more concurrent requests without sacrificing performance.
Union Types Explained
Union Types allow you to specify multiple possible types for function parameters, bringing enhanced flexibility and clarity to the codebase.
- Flexible Function Parameters: In WordPress, functions often receive various input parameters. With Union Types, you can explicitly define multiple types a parameter can accept. This flexibility helps prevent errors and lets your functions handle different data types. For example, a function that accepts a parameter for a post ID could now specify that it accepts either an integer or a string, accommodating different usage scenarios without sacrificing type safety.
- Improved Type Checking: Static analysis tools and IDEs can provide more accurate feedback, suggesting potential issues or offering better auto-completion. This helps you write more reliable code and catch errors. The good news is that the plugin developers can already start taking advantage of the type-checking improvements.
- Future-Proofing: Union Types allow your code to adapt to new features whenever the WordPress core is updated.
Constructor Property Promotion: What is it and why should you care?
This feature simplifies defining and initializing class properties within the constructor, providing several benefits.
- Concise and Readable Code: You can define class properties directly in the constructor’s parameter list, reducing the need for additional property declarations. This results in more concise and readable code, as you can declare and assign values to properties in a single location. This is especially useful when working with classes that have multiple properties.
- Encourages Proper Initialization: Specifying the desired property values as constructor parameters ensures that the properties are set with valid and expected values from the start. This reduces the chances of uninitialized properties causing errors or unexpected behaviors.
- Improved Flexibility: By making certain properties optional, you can create instances of the class with different sets of properties, accommodating various usage scenarios. This can be particularly useful when working with configurable or customizable components.
- Reduced Boilerplate Code: Constructor Property Promotion eliminates the need for explicit property declarations and assignment statements within the constructor. This reduces the amount of boilerplate code you need to write, making your codebase cleaner and more maintainable. It also simplifies adding or modifying class properties, as you only need to update the constructor parameter list.
- Plugin and Theme Development: Developers can streamline the initialization of class properties, resulting in cleaner and more efficient code. This can lead to improved performance, easier maintenance, and enhanced compatibility with other WordPress components.
What’s New in PHP 8.1?
Fibers: What are they and how do they work?
PHP 8.1 introduced Fibers, allowing developers to write asynchronous code in a more readable and familiar way.
- Asynchronous Task Handling: You can use Fibers to perform non-blocking operations, such as making API requests, executing time-consuming database queries, or interacting with external services, without blocking the execution flow.
- Improved Responsiveness: When a user submits a form or triggers a background process, Fibers allows your website to respond to other requests without waiting to complete the long-running operation. This results in a smoother user experience, reduced latency, and better overall performance.
- Readable and Familiar Code: You can structure your code to resemble synchronous operations, making it easier to understand and maintain.
- Plugin and Theme Development: Plugins or themes that involve background processing, real-time updates, or long-running tasks can benefit from utilizing Fibers to enhance their performance and user experience.
Stricter Type Checks: Why they matter
- Enhanced Type Safety: By enforcing stricter type declarations and specifying accurate types for function parameters, return values, and variables, you reduce the chances of runtime errors and unexpected behavior.
- Early Error Detection: We all hate those error messages that we can’t even understand. You can identify type-related errors during development with stricter type checks, providing more accurate error messages and warnings.
- Improved Code Readability: Type declarations offer valuable information about the expected data types and usage of functions and variables. By embracing stricter type checks, your code becomes more self-documenting and easier to understand for yourself and other developers working on the project.
- Efficient Bug Identification: By providing more specific error messages, the exact location of a type violation. This helps you pinpoint and resolve issues more efficiently, saving time and effort during debugging.
- Plugin and Theme Development: Adhering to stricter type declarations, plugin, and theme authors can ensure better compatibility and reduce the risk of type-related conflicts with other plugins or themes.
PHP 8.2: Should you upgrade now?
I would definitely NOT upgrade to PHP 8.2 since it was just released in December 2022. And even though the latest version is PHP 8.2, I still would not upgrade just because it’s the latest and greatest. I think compatibility is certainly more important as I see many complain that certain versions of PHP 8 breaks WordPress. But here are a few features introduced in PHP 8.2 that I am excited about.
Readonly Classes: What are they and why should you use them?
By marking properties as readonly, developers can prevent unauthorized modifications to critical data. This helps protect against accidental or malicious tampering, reducing the risk of security vulnerabilities. Additionally, readonly classes can improve performance by allowing the PHP engine to optimize code execution involving these classes.
Null and False as standalone types: By using Null and False types in type declarations and expressions, developers can enforce more precise data handling and avoid unexpected behaviors caused by implicit conversions. This can really help with debugging.
Disjunctive Normal Form (DNF) types: DNF types enable developers to specify multiple possible types for a variable. In situations where a variable can have different types, such as a string or an integer, DNF types (e.g., string|int) provide a concise and expressive way to define the variable’s possible values. This really reduces type-related errors.
Sensitive parameter value redaction: This helps protect sensitive data from being exposed in stack traces. This is particularly valuable for WordPress plugins and themes that handle confidential information like passwords or credit card numbers. The risk of unintentional data exposure is mitigated by automatically redacting sensitive parameter values in stack traces.
New random number generator: WordPress utilizes random numbers for various purposes, such as generating password reset tokens or nonces. With a more secure and efficient random number generator, WordPress sites can benefit from stronger cryptographic operations and a more reliable generation of unpredictable values.
When it comes down to it, I feel as if the more important thing right now is compatibility. And security, of course, but even though every new version of PHP improves security, any version above PHP 7.4 still receives updates until its end of life.
I also know a lot of web hosts are encouraging their customers to upgrade to PHP 8 and above; I think it is important to remind them that PHP 7.4 is still not at its end-of-life and that WordPress is still technically considered to be in beta for versions of PHP 8 and above. So they really should consider not pressuring you to upgrade just yet. At least until WordPress, which is one of the most popular CMS platforms, is officially out of beta for PHP 8.
It is also important to note that not all WordPress plugins and themes are compatible with the latest PHP versions. If you are using a plugin or theme that is not compatible with the version of PHP you set, you may experience errors or unexpected behavior.