PHP is infamous for releasing new versions every year. And as always, stupendous features are baked into the latest release, opening doors for more possibilities with the language. Many are deliberating whether upgrading to the new version is the right move. Of course, using the latest version is always recommended, but how do you decipher which PHP version is right for your web project? Currently, only 8% of PHP users use the supported 8. x versions.
This article covers the factors you should consider before upgrading your PHP version, the major PHP versions and their peculiarities, how to check and change your PHP version, and many other vitals.
Selecting a PHP hosting provider ensures optimal performance and security for your PHP-based websites and applications. Explore our curated list of top PHP hosting providers to find the best fit for your needs.
Best PHP Hosting Choices
Provider | User Rating | Best For | |
---|---|---|---|
4.8 | cPanel PHP | Visit FastComet | |
4.6 | Managed PHP | Visit Hostinger | |
4.0 | User-Friendliness | Visit IONOS |
Importance of Choosing the Right PHP Version
Whether you’re building the simplest apps like CMSs or complex ones like enterprise applications, using the right PHP version will increase your efficiency and help you create high-quality and cleaner code. Here are a few other reasons:
1. Security Risks Associated with Using an Unsupported PHP Version
Security is, undoubtedly, the most important reason to choose the right PHP version for your web project. Older versions may have known security vulnerabilities that hackers can exploit. To protect your data, ensure you run your code on recent versions since they’re regularly updated with robust security features.
2. Compatibility with CMS and Other Software
Most software and plugins are built on PHP. And when updated, they may require specific PHP version requirements to operate without glitches. If you’re using an outdated PHP version on your web server or the code running on your site uses deprecated features not supported by newer versions, there’s a high chance of experiencing incompatibility issues like website crashes or downtime.
The latest PHP release, PHP 8.0 is almost 3X faster than PHP 7.X. You can access faster and more optimized performance using updated PHP versions. To avoid experiencing incompatibility issues with your software, opt for a hosting provider that supports new PHP versions. Also, check the migration guide to know if your app is compatible with the latest PHP version.
3. Improved Performance
Another vital factor to consider is the performance and speed of your website. Speed facilitates a good user experience, and the PHP version of your web server plays a huge role in impacting your website performance and speed.
Newer PHP versions typically come with performance improvements and optimizations to help your website load faster and handle more traffic. For example, the JIT extension in PHP 8 enables you to create JIT code within PHP, using the Zend Engine II as runtime. It loads fast and uses less memory compared to features in previous releases. However, compare the performance of your current version to the latest one to know which is best for your website.
4. Access to Developer-Friendly Tools
Recent PHP versions have advanced developer-friendly tools to help developers build more efficient websites and applications. This includes new functions, libraries, and other features that make working with PHP code and building complex applications easier.
PHP 7, for example, introduced support for 64-bit architecture, which allowed developers to take advantage of more powerful hardware. Other improvements, such as improved error handling and better support for HTTP/2, have made it easier to integrate PHP with other technologies and services.
Overview of PHP Versions
Aside from Isaac Newton’s discovery of the laws of motion, the invention of this eccentric programming language will definitely intrigue you. In a quest to track visits to his online resume, a Danish Canadian programmer, Rasmus Lerdorf, wrote the PHP code in C language and called it Personal Home Page. Leveraging its success, he rewrote the code to provide better and larger implementations. Then in 1995, he released the source code to the public. This tool is now the backend of up to 80% of all websites today, including Facebook.
PHP is an open-source server-side programming or scripting language. A PHP code comes in two formats: a pure PHP file that contains nothing but PHP code and a part of an HTML file (meaning the PHP code is embedded within the HTML file). This programming language is used to develop web applications.
Brief History of Php Versions
Over the years, PHP has undergone several changes and implementations to provide a better efficient ecosystem for developers. But Rasmus Lerdorf shared that he never intended to create a programming language from inception! The term ‘programming language’ wasn’t used until the PHP 2.0 release.
Today, several developers have contributed to making the language more flexible with significant improvements in security, performance, and functionality. PHP 7, released in 2015, was a major milestone that introduced significant performance improvements and reduced memory usage.
List of Major Php Versions and Their Release Dates
PHP version | Release Dates | End of Life | Key Features |
PHP 7.0 | 3 December 2023 | 10 January 2024 | support for group use declarations, null coalescing operator (??), scalar type and return type declarations. |
PHP 7.1 | 1 December 2023 | 1 December 2023 | The most notable are nullable types, void options, iterable types, multi-catch exceptions, and support for keys in list (), among others. It also introduced support for keys in array destruction, security improvements, and enhanced performance. |
PHP 7.2 | 30 December 2023 | 30 November 2024 | Argon2 in password hash, a more secure alternative to Bcrypt algorithm, parameter type widening fbag allows users to widen the type of a parameter from a parent class interface, encrypted ZIP archives, improved error handling. |
PHP 7.3 | 6 December 2023 | 6 December 2023 | Heredoc and Nowdoc Syntax, advanced JSON support, allow trailing commas in function calls, improved file system functions, support for Multiple Mbstring encodings, etc. |
PHP 7.4 | 28 November 2024 | 28 December 2023 | Arrow functions 2.0, null coalescing operator (), typed properties 2.0, covariance returns and contravariant parameters, etc. |
PHP 8.0 | 26 November 2024 | 26 December 2023 | It introduces JIT (Just-In-Time) compiler, named arguments, and consistent type errors to make it easier to debug codes, construction property promotion, etc. |
PHP 8.1 | 25 November 2024 | 25 December 2023 | It offers enumeration to allow developers to define a set of named constants with a specific type, function signature verification for specifying the type and number of function arguments, resource to object migrations, return types in interfaces and internal classes, etc. |
PHP 8.2 | 23 November 2024 | 8 December | Deprecation of array_combine(), read-only classes, selective parameter redaction support, DNF types, null, false, and true types, and several other new features. |
Factors to Consider when Choosing a PHP Version
When deciding which PHP version to use, you must consider these factors:
1. Compatibility with Cms and Other Software
Compatibility is a crucial factor to look out for when choosing a PHP version for your web project. Most CMSs like WordPress, Drupal, and Joomla are built on PHP and require a certain version of PHP to operate maximally. If your hosting company doesn’t support this PHP’s current or higher versions or your code is built on lesser versions, you may be unable to upgrade your software or access relevant updates.
To ascertain the PHP version of the software or CMS, check the developer’s website or the documentation provided on the website. Using the wrong PHP version for these tools can result in errors and security breaches. To avoid experiencing this, opt for hosting providers that support PHP version control. This way, you can upgrade the version of your web server PHP at will.
2. Performance and Security Updates
It’s a no-brainer that performance and security are top-tier vitals for any web project. Each PHP version released is always an improvement on the previous one, and it’s likely to introduce security and performance features to prevent issues associated with the previous releases. Choosing an updated PHP version with better performance can help you provide a better user experience and protect your website and users’ data from various security threats.
3. Support and Availability of Documentation
Each PHP version’s documents are available in downloadable formats and up to 10 languages on the official website. You’ll also find diverse resources in guides, tutorials, and references for each PHP version, from the first release to the latest.
There are also third-party communities where you can access first-hand help with any version – Sub-Reddit groups, slack channels, PHP online forums, etc.
Differences Between PHP Versions
PHP versions have evolved over the years, with new features following each release. Let’s explore the differences and commonalities between the major versions and how you can use them in web development:
New Features and Changes Introduced in Each Version:
The introduction of new hashing algorithms, encryption libraries, and improved error handling in later versions of PHP make it easier for developers to build more secure applications. For example, the nullable types and iterable pseudo-type introduced in PHP 7.1 make it easier for developers to handle complex data structures and reduce code complexity.
These new features also spurred faster performance, reduced memory usage, and more efficient resource utilization. Here are other exciting features in PHP 8.x versions:
- Union Types: This feature allows developers to specify that a parameter or return type can be one of several possible types. For example, a function can accept either an integer or a string.
- Named Arguments: With named arguments, developers can pass arguments to functions using the name of the argument instead of its position. This makes code more readable and helps prevent errors when functions have many arguments.
- Attributes: This allows developers to add metadata to classes, functions, and other language constructs. This metadata can be used by tools like IDEs or static analysis tools to provide additional information about the code.
- Match Expressions: This is similar to switch statements, but they offer more flexibility and allow developers to use expressions instead of just simple values.
- Constructor Property Promotion: This feature allows developers to declare and initialize class properties in the constructor, making code more concise and easier to read.
Deprecated Features and Changes that May Affect Compatibility:
Deprecated features are functions, tools, or programming elements formally supported but no longer recommended. They’re mostly retained for backward compatibility features but will be removed from future versions, leading to compatibility issues for applications using them. For instance, in PHP 8.0:
- The MySQL extension was removed
- The create_function() function was removed
- The each() function was removed
- The strip_tags() function now throws an error with an invalid argument
To address deprecated features, developers should review their code and find alternative solutions. This could involve using new functions or constructs that have replaced the deprecated features, or it may require rearchitecting application parts. Developers should also keep up-to-date with new language versions and ensure they use the latest version supporting their application.
Examples of Deprecated Features and how to improve the code:
Here are a few examples of deprecated features in PHP and how to update code to avoid compatibility issues:
- Example 1: Using the ereg functions
In PHP 7.0, the ereg functions were deprecated in favor of the preg functions. If you have code that uses the ereg functions, update it to use the preg functions:
// Before $result = ereg_replace("[[:digit:]]+", "", "My phone number is 555-1234.");// After $result = preg_replace("/[[:digit:]]+/", "", "My phone number is 555-1234.");
- Example 2: Using the __autoload() function
In PHP 7.2, the __autoload() function was removed. If you have code that uses __autoload(), update it to use the spl_autoload_register() function:
// Before function __autoload($class_name) { include $class_name . '.php'; }// After spl_autoload_register(function ($class_name) { include $class_name . '.php'; });
- Example 3: Using the create_function() function
In PHP 7.2 and PHP 8.0, the create_function() function was removed. If you have code using create_function(), update it to anonymous functions:
// Before $function = create_function('$a,$b', 'return $a + $b;');// After $function = function ($a, $b) { return $a + $b; };
- Example 4: Using the $HTTP_RAW_POST_DATA variable
In PHP 8.0, the $HTTP_RAW_POST_DATA variable was removed. If you have code that uses $HTTP_RAW_POST_DATA, update it to php://input instead:
// Before $data = $HTTP_RAW_POST_DATA;// After $data = file_get_contents('php://input');
How to Check PHP Versions
There are several ways to check the PHP versions of your web server or application:
1. Checking the Current Php Version
- Using phpinfo(): If you have access to your server, create a PHP file and run it on your server
<?php phpinfo(); ?>
Save this file as ‘phpinfo.php’ and upload it on your web server. Then, open your web browser and the full address in the search bar. You’ll see detailed information about your PHP version, when it was created, configuration settings, system information, copyright data, etc.
- Using the command line (Windows, Linux, and macOS): If you have SSH access to your web server, use the command line to check your PHP version. This can also be used to check the PHP version installed locally. It is also useful when working with PHP scripts that run from the command line like cron Job or command-line utilities. To check your PHP version using this method, run this command in your web server:
php -v
You’ll see the PHP version number, copyright information, build date and time, etc. If you’ve previously installed more than one PHP version on your server, this command will display the default CLI version.
- Using PHP code snippet: this method is easy and almost similar to the first one discussed in this section. The code snippet uses the predefined PHP constant ‘PHP_VERSION’ to get your current PHP version. Save this code as a PHP file and upload it to your web server:
<?php echo "PHP version: " . PHP_VERSION . "n"; ?>
Open your web browser and type the full address on the search bar:
http://www.example.com/phpinfo.php
The output shows only the PHP version.
- Via cPanel: If you’re using WordPress or another CMS that supports the cPanel control panel, log into your cPanel and search for the software section. Click on ‘select PHP version.’ After this, you’ll be redirected to a page showing your PHP version and a handful of other versions the hosting provider supports.
How to Interpret the Results
The PHP version comprises three parts: major, minor, and patch level. The major version is the significant changes made to the PHP language, and the minor version indicates new features. The patch level is used for bug fixes and security updates.
For instance, in PHP version 7.1.3, the major version is 7, the minor version is 1, and the patch level is 3. If you have an application running on PHP 5.0, it may not work with PHP 7.0 because there have been significant changes between the versions. But an application developed with PHP 7.2 will work with PHP 7.4 since they’re within the same major version.
2. Changing the PHP Version Using cPanel or Command Line
To change the PHP version of your web server, follow these two steps:
How to change PHP version using cPanel:
- Log into the cPanel of your hosting provider and navigate to the software section
- Click on the ‘MultiPHP manager’
- You’ll be redirected to another page. Here, choose the domain to which you want to change the PHP version
- Then, click on the menu button to select your desired PHP version
- Lastly, click ‘apply’
How to change the PHP version using the command line
- SSH into your server
- Check the current PHP version of your server by running this command:
sudo update-alternatives --display php
- Install the desired PHP version single PHP package manager such as apt or yum
- Switch to the desired PHP version by running this command:
sudo update-alternatives --set php /usr/bin/phpX
- Replace the value X for your desired PHP version
- Save the value with CTRL O and then click Enter
To verify that your PHP version has been successfully changed, run php -v on your web server.
php -v
Conclusion
Finding the right and compatible PHP version for your web project is straightforward. With the updated versions, you have better tools in your arsenal to create clear, cleaner, and maintainable code. Always ensure you do proper testing to affirm that everything in your application is compatible with the new upgrade. Also, read the migration guide and seek help from experts if necessary to avoid compatibility issues with your existing code and security vulnerabilities.
Next Steps: What Now?
- Check the PHP migration guide to ensure your system is compatible with the new PHP version
- Opt for a web hosting provider that supports the latest php release
- Stay informed by reading documentation from the PHP official website and other third-party authentic sites
- If you’re currently working on an existing project, How to Secure PHP Integrated Into Your Web Page with Configuration Settings