facebook What is new in PHP 8.1 and how to upgrade to this latest version?

What’s new in PHP 8.1 and how to upgrade to the latest version of PHP?

Summary: People, processes, gadgets, things – everything has a “date of expiry.” Call it death, burnout, obsoletion, redundancy, or a stage of retirement. Technology is no exception. Software, languages, frameworks, etc., unless updated and taken in their latest forms, will not yield the result they are expected or designed for. Below is a glimpse at the “retirement date” of PHP 7.4 and PHP 8.0 and the arrival of PHP 8.1.

For most of us, PHP needs no introduction. As of November 2022, about 77.5% of all websites (where the server-side language is known) use PHP – Facebook, WordPress, Wikipedia, and Slack included.

Percentage of websites using different versions of PHPPercentage of websites using different versions of PHP

At some point, “personal home page” was what one would have understood from ‘PHP.’ Technically, PHP denotes “Hypertext Preprocessor.” An open-source language, PHP is free to use and is a general-purpose scripting language for web development.

Why do you need to upgrade to PHP 8.1?

Each year sees new versions of PHP released with new features and upgrades. Three versions of PHP are currently out there – PHP 7.4, PHP 8.0, and PHP 8.1.

By the end of November 2022, PHP 7.4 will lose active support from PHP Group.
Time to upgrade to PHP 8.1 – unless you want to risk your website to security vulnerabilities and performance issues.
Take a look at these details-
Active Support for different PHP versions
Or, for a more convenient understanding, look at this snapshot in a calendar style:
Support for different PHP versions calendar style
Where ‘active support’ implies a version or release actively supported. Security issues are dealt with, and regular point releases are made too. Security support means that a version is supported for crucial security issues only. Therefore, releases will be done only on a need basis. A version that is not supported at all is said to be in its ‘end of life’ stage. Any version that is in its end-of-life stage needs to be actively upgraded.

15-Point web app development checklist


We respect your privacy. Your information is safe.

What are the benefits of upgrading to PHP 8.1?

1.Security

Security is the first word that comes to mind! Have you heard of unpatched security vulnerabilities? These “remain the most prominent attack vectors exploited by ransomware groups” (Ransomware Spotlight Year End Report, 2022). Unless you upgrade to PHP 8.1, you risk exposing your website to unpatched security vulnerabilities.
Some security issues are XSS, directory traversals, retrieval, directory traversal, memory corruption, overflow, code execution, and DoS.
PHP vulnerabilities by type
Consider how open-source PHP platforms have re-oriented and revolutionized eCommerce platforms. Then imagine WooCommerce, Drupal, Magento, Joomla, CodeIgniter, or Symfony 3 suddenly being exposed to security threats, bringing into danger every site that operates on these. Unless a mandatory upgrade is done to PHP 8.1, your eCommerce websites stand exposed to security threats.

2.Performance

Performance improvement is a natural benefit of an upgrade. With each new version of PHP, performance has been boosted – When PHP 5.6 ruled, PHP 7.0 brought in better performances, PHP 8.0 brought improvements, and now PHP 8.1 has continued the tradition.
Look at this, for example – WordPress powers more than 43% of all websites on the internet. The following is an example of tests done in terms of ‘requests per second’ as a PHP benchmark. More the number of requests, the better it is.
WordPress 5.9 RCT2

3.Support

Ongoing Support from creators is a nice-to-have or must-have depending on the adopter’s in-house tech capabilities & specific situation. When a version is supported, developers can monitor and improve the protocols. Updating to the latest versions for security and removing flaws and configuration failures are important components for gaining compatibility and are needed for full support while working on a particular version.

4.Improved Features

Better features are a major and self-evident reason developers recommend shifting to PHP 8.1. There are new features like Enums, Fibers, never return types, Intersection Types, readonly properties, and performance improvement with inheritance cache. You also get an inheritance cache and internal function optimization like defined final class constants, explode, and file uploads from strings.
A glance at the last three versions of PHP:
A glance at three versions of PHP

How to check which PHP version you are on?

In case you want to check which PHP version you are on, you could do the following:

  • Check your hosting panel (cPanel, Plesk, Webmin, or hPanel) under the hosting section to check which version your website is currently running on, and also to select which version you want to switch to.
  • Check WordPress dashboard – For WP above 5.0, you can check it in the Site Health section
  • Use online tools like Google Chrome DevTools, and Pingdom. You need to check with your hosting provider if security concerns make this difficult.
  • Check by uploading the relevant file to your server.

Create a blank file phpinfo.php
Paste the following-

Inspite of awareness of the need to switch to the latest PHP version, the actual adoption could be slow because of

  1. Low awareness levels
  2. No direct control over the upgrade
  3. Time required to upgrade and test compatibility
  4. Fear of a possible breakdown in the website and need for extensive support

What is new in PHP 8.1?

There are many new features PHP 8.1 brings, besides an overall improved performance, better security, deprecation of some functionalities, and defensive programming.

Significant features of PHP 8.1

1. Enums

“Enums,” short for Enumerations, are a special kind of object. They are types that can have a fixed number of possible values.

2. Readonly Properties

This prevents modification of properties after initialization (after a value has been assigned to them).

3. First-Class Callable Syntax

This implies getting a reference to any function.

4. Pure Intersection types

Intersection type declarations accept values that can satisfy multiple class-type declarations rather than single ones. At present, it is not possible to mix intersection and union types.

5. Never Return Type

When a function or method is declared with the never type, it will not return a value. In this case, it can either throw an exception or end the script’s execution with a call of die ( ), exit ( ), or trigger_error ( ).

6. Fibers

Fibers are actively used by ReactPHP and Amphp. They help create blocks you can pause and resume, like Generators. They can be picked up from anywhere in the stack.

7. Explicit Octal numeral notation

PHP 8.1 allows writing octal numbers with an explicit 0o/0O prefix. This is similar to binary and hexadecimal integer literals.

8. Array unpacking support for string-keyed arrays

Array unpacking was present in PHP 7.4 also. Still, it was functional only with numeric keys – now, unpacking can also be done with string keys.

9. New classes, functions, and interfaces

PHP 8.1 introduces the new #[ReturnTypeWillChange]attribute. It also brings on certain new functions – fsync,fdatasync,array_is_list, and Sodium XChaCha20.

Performance-related features in PHP 8.1

(Source: PHP)

  • JIT backend for ARM64 (AArch64)
  • Inheritance cache (avoid relinking classes in each request)
  • Fast class name resolution (avoid lowercasing and hash lookup)
  • timelib and ext/date performance improvements
  • SPL file-system iterators improvements
  • serialize/unserialize optimizations
  • Some internal functions optimization (get_declared_classes(), explode(), strtr(), strnatcmp(), dechex())
  • JIT improvements and fixes

Performance-related improvements as compared to PHP 8.0

PHP 8.1 shows the following additional improvements as compared to PHP 8.0

  • A speedup of 23.0% in the Symfony Demo
  • Speedup of 3.5% in WordPress

Performance improvement

Deprecated and backward compatibility breaks in PHP 8.1 (Source: PHP)

Following is the deprecated list (Source: PHP)

  • Passing null to non-nullable internal function parameters is deprecated.
  • Tentative return types in PHP built-in class methods
  • Serializable interface deprecated.
  • HTML entity en/decode functions process single quotes and substitute by default.
  • $GLOBALS variable restrictions.
  • MySQLi: Default error mode set to exceptions.
  • Implicit incompatible float to int conversion is deprecated.
  • finfo Extension: file_info resources migrated to existing finfo objects.
  • IMAP: imap resources migrated to IMAP\Connection class objects.
  • FTP Extension: Connection resources migrated to FTP\Connection class objects.
  • GD Extension: Font identifiers migrated to GdFont class objects.
  • LDAP: resources migrated to LDAP\Connection, LDAP\Result, and LDAP\ResultEntry objects.
  • PostgreSQL: resources migrated to PgSql\Connection, PgSql\Result, and PgSql\Lob objects.
  • Pspell: pspell, pspell config resources migrated to PSpell\Dictionary, PSpell\Config class objects.
    

        

Hire PHP Developers for Your Next Project

        Request Free Consultation
    

    

        contact net solutions
    

Steps to upgrade to PHP 8.1

So you’ve heard about PHP 8.1 as the latest version but now want to upgrade and install it? Here’s a broad idea of how to do it-

1. System Update

The first step is to update system repositories and dependencies. You can use the following command to do this.
sudo apt update

2. Update the server with the latest PHP dependencies

Mostly, a Ubuntu or Debian-based server is used these days.
Use the given commands as required:

Ubuntu 21.04, 20.04, 18.04:

sudo apt update

sudo apt install lsb-release ca-certificates apt-transport-https software-properties-common -y

sudo add-apt-repository ppa:ondrej/php

Debian 11, 10, 9:

sudo apt install -y gnupg2 ca-certificates apt-transport-https software-properties-common

wget -qO – https://packages.sury.org/php/apt.gpg | sudo apt-key add –

echo “deb https://packages.sury.org/php/ buster main” | sudo tee /etc/apt/sources.list.d/php.list

sudo apt update

Install PHP 8.1

  • Update the latest dependencies & repositories
  • Use the following common to install PHP 8.1
  • sudo apt install php8.1

    sudo apt install php8.1-common php8.1-cli

  • After hitting the above command, you will get this:
  • Install PHP 8.1 Part 1

  • Type ‘Y’
  • Hit ‘enter’ to install PHP 8.1
  • Use this command to check whether PHP 8.1 is installed successfully
  • sudo php -v
    Install PHP 8.1 Part 2

  • Use this command to set up modules and extensions
  • sudo apt install php8.1-{imagick,bz2,curl,intl,mysql,readline,xml,fpm,mbstring,zip,bcmath}

How do you change from an older version of PHP to the latest version of PHP 8.1?

Apache server

  • Disable and replace the older version with the latest version of PHP 8.1
  • sudo a2dismod php8.0

  • Use this command to enable the latest version
  • sudo a2enmod php8.1

  • Check syntax error
  • sudo apache2ctl -t

Check Syntax Error

Nginx server

Reasons related to performance and reverse proxy lead developers to choose Nginx over Apache.

  • Just install the latest PHP as mentioned above
  • Edit the PHP-FPM socket path in the Nginx Server Block
  • Edit the Server Block located in the /etc/nginx/sites-available/ directory using the command

sudo nano /etc/nginx/sites-available/default
Edit the Server block

Before you edit the Nginx Default Server Block,

  • Comment out (by removing the pound/hash sign) and edit the PHP-FPM path php7.4-fpm
  • Replace it with the latest PHP version number as this
  • – php8.1-fpm

  • Type CTRL+X, then Y, and hit Enter key
  • Use the following command to check syntax error
  • sudo nginx -t
    Check the syntax error again

  • Restart the Nginx server with the command
  • sudo service nginx restart
  • This enhances performance on the front end too.

RISK ALERT: What is the risk of not upgrading to PHP 8.1?

Not switching to the latest PHP version – in this case, PHP 8.1 now – would mean exposing your website to security issues and lower speed and performance.

Frequently Asked Questions

  • 01

    Is PHP 8.1 compatible with WordPress?
    WordPress is compatible with PHP versions as old as PHP 5.6. Implying thereby that WP works with PHP 7.0, 7.1, 7.2, 7.3, 7.4, 8.0, and now PHP 8.1. However, versions before PHP 7.4 are end-of-life (EOL) and do not receive updates, thus exposing sites to security vulnerabilities. Upgrading to PHP 8.1 for better security and enhanced performance is essential.
  • 02

    Is PHP 8.1 the latest version of PHP?
    Come December 8, 2022, PHP 8.2 will be released. Features of version 8.2 were frozen in July 2022. The primary features of PHP 8.2 are readonly properties, standalone types, and further performance improvements.
  • 03

    Is PHP 8.1 backward compatible?

    PHP 8.1 deprecates some features and functions from the earlier versions. This implies that there would be an issue with backward compatibility if we implement the PHP 8.1 union types. This will break on all lower versions. As such, backward compatibility is an issue with all PHP versions.

  • 04

    What are the reasons for upgrading to PHP 8.1?
    • Greater efficiency and capability to handle more complex scripts
    • Many new features that reduce operational costs
    • Greater opportunities for PHP developers
    • Improved performance over the previous versions

Looking for a scalable PHP-based solution?


Surabhi Shukla

About the Author

Surabhi Shukla is a dedicated PHP developer with a special forte as a Magento Certified Developer. Outside of her professional realm, Surabhi has a penchant for listening to jazz music and indulging in speed reading. However, she doesn't quite fancy podcasts that tend to drag on.


While she is still looking for her definitive claim to fame, she is known for being an "ever-dependable" team player. Problem-solving comes naturally to her. Her eye for detail and strategic thinking have lent perfection to many projects that she has worked on.

Pin It on Pinterest