Get "PHP 8 in a Nuthshell" (Now comes with PHP 8.3)
Amit Merchant

Amit Merchant

A blog on PHP, JavaScript, and more

What's new in PHP 8.4

PHP 8.3 has already been released a few months back and it’s time to look at what’s coming in PHP 8.4.

It’s not a lot of features so far but there are some interesting ones. I’ll be updating this article as more features are added to the PHP 8.4 release. So, make sure you bookmark this article.

New modes for the round() function

The round() function is used to round a number to its nearest integer. It’s a very common function used in PHP.

Up until now, the round() function had only four rounding modes: PHP_ROUND_HALF_UP, PHP_ROUND_HALF_DOWN, PHP_ROUND_HALF_EVEN, and PHP_ROUND_HALF_ODD.

In PHP 8.4, four more rounding modes will be added to the round() function.

  • PHP_ROUND_CEILING - This will round the number to the nearest integer bigger than the given number. So, round(1.3, 0, PHP_ROUND_CEILING) will return 2 since 2 is the nearest integer bigger than 1.3.

  • PHP_ROUND_FLOOR - This will round the number to the nearest integer smaller than the given number. So, round(1.3, 0, PHP_ROUND_FLOOR) will return 1 since 1 is the nearest integer smaller than 1.3.

  • PHP_ROUND_AWAY_FROM_ZERO - This will round the number away from zero. So, round(1.3, 0, PHP_ROUND_AWAY_FROM_ZERO) will return 2 since 2 is away from zero and is the nearest integer.

  • PHP_ROUND_TOWARD_ZERO - This will round the number towards zero. So, round(1.3, 0, PHP_ROUND_TOWARD_ZERO) will return 1 since 1 is towards zero and is the nearest integer.

Read RFC

A new way to disable JIT

PHP 8.0 introduced JIT (Just In Time) compilation which is a technique that converts PHP code into machine code at runtime. This improves the performance of PHP code.

So, you can change a lot of different configurations related to JIT in the php.ini file but the current way of disabling JIT is a little bit confusing.

You can do it by setting the opcache.jit_buffer_size to 0 in the php.ini file. But this is not very intuitive.

opcache.jit=tracing
opcache.jit_buffer_size=0

To make it more intuitive, PHP 8.4 will introduce a new configuration directive called disable which will disable JIT.

opcache.jit=disable
opcache.jit_buffer_size=64m

Here, the default value for opcache.jit_buffer_size is also updated to 64m from 0.

Read RFC

A new JIT implementation

This change is not a user-facing change but it’s worth mentioning here.

PHP 8.4 will attempt to implement a new JIT implementation based on the IR (Intermediate Representation) framework. This new implementation is said to be “smarter” than the current one.

This implementation only uses a single back-end that constructs IR to generate x86 and AArch64 code as opposed to the current implementation which uses two back-ends.

Read RFC

A new option to parse huge XMLs

PHP 8.4 will introduce a new option called XML_OPTION_PARSE_HUGE that can be passed along to xml_parser_set_option while parsing XMLs.

Here’s how this looks like.

function startElement($parser, $name, $attrs)
{
    // Do something interesting
}

function endElement($parser, $name) 
{
    // Do something interesting
}

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_PARSE_HUGE, true); 
// Changing this to false, or not executing this line, 
// will cause the parsing to error out on large inputs

xml_set_element_handler($parser, "startElement", "endElement");
// Add more handlers

$success = xml_parse($parser, $my_long_xml_input_already_in_memory);

This option will allow you to parse huge XMLs without running into memory issues or running into parsing errors.

Read RFC

Multibyte equivalents for the trim() function

PHP has always been lacking multibyte equivalents for the trim() function. But in PHP 8.4, this will be fixed.

The following three functions will be added to PHP 8.4.

  • mb_trim() - Same as trim() but for multibyte strings. It will remove all whitespace characters from the beginning and end of a string.

Here’s the entire signature of the function.

function mb_trim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}"): string
  • mb_ltrim() - Same as ltrim() but for multibyte strings. It will remove all whitespace characters from the beginning of a string.

Here’s the entire signature of the function.

function mb_ltrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string
  • mb_rtrim() - Same as rtrim() but for multibyte strings. It will remove all whitespace characters from the end of a string.

Here’s the entire signature of the function.

function mb_rtrim(string $string, string $characters = " \f\n\r\t\v\x00\u{00A0}\u{1680}\u{2000}\u{2001}\u{2002}\u{2003}\u{2004}\u{2005}\u{2006}\u{2007}\u{2008}\u{2009}\u{200A}\u{2028}\u{2029}\u{202F}\u{205F}\u{3000}\u{0085}\u{180E}", ?string $encoding = null): string

Read RFC

A new class for parsing and serializing HTML5

Up until now, PHP’s DOM extension is kind of stuck with the HTML4 specification. That is the DOMDocument class can only parse and serialize HTML4 documents.

But since HTML5 is the new standard, PHP 8.4 will introduce a new class called DOM\HTMLDocument that can parse and serialize HTML5 documents.

Here’s how this looks like.

namespace DOM {
	// The base abstract document class
	abstract class Document extends DOM\Node implements DOM\ParentNode {
		/* all properties and methods that are common and sensible for both XML & HTML documents */
	}
 
	final class XMLDocument extends Document {
		/* insert specific XML methods and properties (e.g. xmlVersion, validate(), ...) here */
 
		private function __construct() {}
 
		public static function createEmpty(string $version = "1.0", string $encoding = "UTF-8"): XMLDocument;
		public static function createFromFile(string $path, int $options = 0, ?string $override_encoding = null): XMLDocument;
		public static function createFromString(string $source, int $options = 0, ?string $override_encoding = null): XMLDocument;
	}
 
	final class HTMLDocument extends Document {
		/* insert specific Html methods and properties here */
 
		private function __construct() {}
 
		public static function createEmpty(string $encoding = "UTF-8"): HTMLDocument;
		public static function createFromFile(string $path, int $options = 0, ?string $override_encoding = null): HTMLDocument;
		public static function createFromString(string $source, int $options = 0, ?string $override_encoding = null): HTMLDocument;
	}
}
 
class DOMDocument extends DOM\Document {
	/* Keep methods, properties, and constructor the same as they are now */
}

Read RFC

Learn the fundamentals of PHP 8 (including 8.1, 8.2, and 8.3), the latest version of PHP, and how to use it today with my new book PHP 8 in a Nutshell. It's a no-fluff and easy-to-digest guide to the latest features and nitty-gritty details of PHP 8. So, if you're looking for a quick and easy way to PHP 8, this is the book for you.

Like this article? Consider leaving a

Tip

👋 Hi there! I'm Amit. I write articles about all things web development. You can become a sponsor on my blog to help me continue my writing journey and get your brand in front of thousands of eyes.

Comments?