img-1

Improving Website Performance with PHP 8.3

With many new features and bug fixes, PHP 8.3 has shown performance improvements compared to its previous versions. If you are using any of these popular frameworks such as Laravel, Symfony, and CodeIgniter, or CMS such as WordPress, WooCommerce, Drupal, etc, you are using PHP indirectly and upgrading to the latest version of PHP 8.3.2, you can get better performance for your websites.

php 8.3 performance

Performance Improvements in PHP 8.3

The latest version has many performance improvements. Here we are going to take a look at the 5 most significant benefits one will practically get after migration.

Error Flags in File

The file() function which is used to read the entire file into an array, is now about 7% faster when checking error flags.

Cache in Getting HTML Node List

Caching The DOMNodeList() method has now a built-in cache. This method is used to get all nodes matching a selection criteria. In the older versions, getting the node list’s time complexity was always n*n. With this change, only the first call will take the same time but all subsequent results will be returned using cache if the DOM remains unaltered.

Document

Get The Best PageSpeed Score

For Your WordPress Website

All in One Optimization Plugin
No Coding Knowledge Required

Below is an example where we will get a list of all JavaScript nodes and then defer them. Deferring JavaScript including jQuery gives a faster page load time and a better PageSpeed Insights score.

<?php

//get the HTML to be sent to the browser
$htmlString = '<!html>.......</html>';

//prepare the DOM
$domDocument = new DOMDocument;
$domDocument->loadHTML($htmlString);

//get all JS nodes
$scriptNodes = $domDocument->getElementsByTagName('script');
foreach ($scriptNodes as $scriptNode) {
     $scriptNode->setAttribute('defer', 'true');
}


?>

Less Memory When Getting Node Content

When getting the text content from nodes, additional memory allocation was happening in the older versions which are removed in PHP 8.3. The textContent attribute of a DOMElement or a DOMNode returns the text content of the current node and its descendant nodes.

The below example code explains how to get the SEO title tag of an HTML page-

//get the HTML to be sent to the browser
$htmlString = '<!html>.......</html>';

//prepare the DOM
$domDocument = new DOMDocument;
$domDocument->loadHTML($htmlString);

$list = $domDocument->getElementsByTagName("title");
if ($list->length > 0) {
    $title = $list->item(0)->textContent;
    if (!empty($title)) {
        echo $title;
    }
 }

?>

Unit Time Performance in Removing Node

The DOMChildNode::remove function now takes only a unit of time, O(1).

Faster Validation of JSON String

There was no way to directly validate a JSON string other than decoding it first and then seeing if the decode was successful or failed. The decoding was a memory-intensive operation as it has built the entire array or object structure. A new method is now available json_validate that is much lightweight to validate the data.

However, after validating a JSON syntax using the json_validate function, you would want to decode it to see the payload, it’s better to avoid calling json_validate and directly call json_decode.

Other New Features in PHP 8.3

Along with the performance boost, there are some noticeable new features added in the PHP 8.3 version.

Better Syntax

With better syntax highlights and notices, it’s easier to see warnings in the IDE itself and catch potential bugs early in the development process. First class callable syntax, was introduced in v 8.1 and has been greatly improved in the latest PHP 8.3 version.

<?php

function sum(int|float $x, int|float $y): int|float {
    return $x + $y;
}

?>

Improved Types Safety

Class, interface, trait, and enum constants now support type. For example, the below code is going to give an error because of the type being changed in the child class B.

<?php
class A {
    public const int CONST1 = 1;
}

class B extends A {
    # expect error here becasue of datatype mismatch
    public const string CONST1 = 'a';
}

class C extends A {
    # expect error too here because the datatype is not specified
    public const CONST1 = 0;
}

?>

Amendments in Readonly Class and Properties

With the new version, non-read-only classes can extend read-only classes. Also, the read-only properties can be reinitialized while cloning the objects. The reinitialization of each property can be done only once to the cloned object.

New Properties And Functions

  1. Properties DOMNode::$parentElement, and DOMNode::$isConnected have been added.
  2. New ldap_connect_wallet(), and ldap_exop_sync() functions for interacting with LDAP servers.
  3. mb_str_pad() function to pad a multibyte string
  4. A new function getArchiveFlag() for ZipArchive to check if a flag is set or not. For example, the Zip archive is read-only, torrent-zip format, etc.
  5. The system functions like posix_sysconf() to get system runtime-related information. For example, you can get the number of active CPUs using posix_sysconf(POSIX_SC_NPROCESSORS_ONLN);
  6. Increment or decrement alphanumeric string using str_increment() and str_decrement() functions

Features Removed in PHP 8.3

The opcache.consistency_checks directive is removed completely in PHP 8.3, though it was depreciated first in version 8.1.18. This directive was used to let OPcache verify the cache checksum for every N request, where N is the value of this configuration directive. Enabling this in the production environment was causing performance issues. It’s recommended to use opcache.revalidate_freq directives instead.

Calling get_class() and get_parent_class() without arguments is now deprecated. You need to pass the object or class name to operate on.

A Brief History of PHP

Ever since it came into existence, its popularity kept increasing in developing websites for which HTML used to be the markup language. PHP came as a “Hypertext Preprocessor” and generating HTML using the script became even easier. The first few versions were not object-oriented but got support for it later. Developers can write C or C++ extensions to add functionalities to the language.

PHP is still very popular and is used as the server-side programming language in over 76.6% of all websites where the technology is known. Around 10 years ago, this percentage was just ~40%.

Major Frameworks Based on PHP

There are many frameworks based on PHP, the very popular ones are – Laravel, Symfony, CodeIgniter, Zend, CakePHP, Phalcon, etc.

Laravel

Laravel is the most popular MVC-based PHP framework that is currently used by approximately 650,000 websites. Laravel offers many security features out of the box and additional helpers to quickly set up the application structure. The object-relational mapper (ORM) library is available to easily exchange data with the MySQL database.

Laravel’s latest version at the time of writing this article is v10.3.2 which requires PHP version 8.1 or higher. Upgrading to the latest version can benefit from many performances.

Laravel however lacks Core Wev Vitals optimization features. This can be achieved along with CDN and caching for Laravel, Image Optimization, and lazy load JS/CSS files by using RabbitLoader.

Optimizing Laravel website performance with RabbitLoader

Symfony

Symfony is another open-source PHP application framework. Applications like Spotify are built on Symfony and can handle thousands of requests every second. Symfony has many rich set features available out of the box. To access data objects, Symfony has a Doctrine object-relational mapper (ORM) that takes care of query building, database abstraction, and results caching.

CodeIgniter

Codeigniter is another popular lightweight framework that is a little easier to start with. The framework has a significant user community, but its ecosystem is relatively smaller compared to other frameworks like Laravel and Symfony.

Major CMS Software Built on PHP

There are many content management systems built on top of PHP, like MediaWiki, WordPress, WooCommerce (a popular WordPress e-commerce plugin), Drupal, Joomla, etc.

WordPress

WordPress is the most popular CMS and is being used by 62% of the total CMS users. It was created to publish blogs but being so convenient to use, it soon became the preferred choice to create several other types of websites. Many hosting services provide a one-click installation and also managed services for premium users.

The marketplace as it grew, started having thousands of plugins and themes for others to use. Installing some plugins may cause slowness also and poor performance. RabbitLoader provides an all-in-one performance optimization plugin for WordPress.

Optimizing WordPress website performance with RabbitLoader

WordPress recommends using PHP version 7.4 or higher and support for the latest PHP 8.3 is included in the WordPress 6.4 version but is in the beta phase. WordPress users should limit to use of PHP versions up to 8.2.

WooCommerce

WooCommerce is a plugin that can be installed on any WordPress site to transform it into an e-commerce application. The application is written in PHP but also uses NodeJS for development.

optimize-woocommerce-php 8.3

Drupal

Drupal is another PHP-based content management system. Drupal’s modular architecture allows users to create more tailored websites and applications to their specific needs. With thousands of modules and themes available, users can extend Drupal’s core functionality to suit various requirements.

While Drupal’s market share is way less than WordPress’s, it is used by at least 14% of the top 10,000 websites worldwide. Some of the notable users are – NASA, UNICEF, VISA, etc.

Drupal’s latest release v10.2.3 was done on 7th February 2024. To migrate to this version, you would need at least PHP 8.1 or higher. Drupal 10 also recommends Composer version 2.3.6 or higher, which is required for compatibility with PHP 8.2.

Conclusion

Users should migrate to the latest version of PHP 8.3 to gain performance benefits. WordPress users need to wait for some time to be fully compatible with PHP 8.3

Document

Get The Best PageSpeed Score

For Your WordPress Website

All in One Optimization Plugin
No Coding Knowledge Required