PHP 8.4 is set to introduce a variety of new features and updates aimed at enhancing security, improving functionality, and providing developers with new tools to efficiently manage and process data.
If you want to test it in advance, you can consult the release calendar:
What’s new in PHP 8.4
Increased bcrypt cost
The default cost of bcrypt has been increased to 12.
Bcrypt serves as the password hashing function utilized by PHP. Acting as a robust shield, it fortifies against hackers attempting to crack passwords.
This change will improve security, but you will lose a few milliseconds when hashing passwords.
If you are interested, you can check the following RFC for performance testing:
Enhance large XML documents parsing
In PHP 8.4, a new parser option was introduced to address an issue in the ext/xml
PHP extension related to parsing large XML documents.
The new parser option resolves this issue, enabling developers to parse large XML documents effectively without complex workarounds.
Learn more: PHP RFC: XML_OPTION_PARSE_HUGE
Multibyte trimming functions
In PHP 8.4, the mbstring
extension introduces three new functions: mb_trim()
, mb_ltrim()
, and mb_rtrim()
.
These functions simplify trimming strings containing multibyte characters, replacing the previous workaround of using regex with preg_replace()
.
They handle whitespace and other characters in a multibyte-safe manner. By default, they remove a predefined set of characters, including various types of space characters, but not all Unicode characters.
Functions in PHP 8.4 and their default behaviors:
mb_trim($string, $characters)
: Trims characters from both ends of a string.mb_ltrim($string, $characters)
: Trims characters from the beginning (left side) of a string.mb_rtrim($string, $characters)
: Trims characters from the end (right side) of a string.
By default, $characters
includes a variety of whitespace characters, but not all possible Unicode characters due to storage and compatibility concerns.
Learn more: PHP RFC: mb_trim
DOM extension now supports HTML5
In PHP 8.4, the DOM extension received a significant upgrade, now supporting HTML5 parsing and serialization. With the new DOM\HTMLDocument
class, handling HTML5-specific tags or embedding HTML in JavaScript becomes hassle-free, ensuring compatibility with modern web standards.
use DOM\HTMLDocument;
$htmlDocument = HTMLDocument::createFromString('<!DOCTYPE html><html><body>Hello, Medium!</body></html>');
Enhancements to DateTime and DateTimeImmutable
PHP 8.4 brings significant improvements to the DateTime
and DateTimeImmutable
classes, enhancing their precision and versatility for developers working with date and time data.
They have a new method named createFromTimeStamp creates an instance from a given UNIX timestamp as an integer or a float value.
$dt = DateTimeImmutable::createFromTimeStamp(1703155440);
$dt->format('Y-m-d'); // "2023-12-21"
$dt = DateTimeImmutable::createFromTimeStamp(1703155440.628);
$dt->format('Y-m-d h:i:s.u'); // "2023-12-21 10:44:00.628000"
The include also the addition of $getMicrosecond() and $setMicrosecond() methods, enabling high-resolution time measurement for applications such as logging. These enhancements simplify time data management by providing direct access to microseconds, supporting more accurate time stamping and aligning PHP’s capabilities with other high-level languages
Implicitly Nullable Parameter Declarations Deprecated
In PHP 8.4, a significant deprecation impacts implicitly nullable parameter declarations, signaling a notable change that may result in deprecation notices in legacy PHP applications. The recommended approach is to to explicitly declare nullable types using the ‘?’ prefix to enhance clarity and maintain compatibility
Change the implicit nullable type declaration to a nullable type declaration:
Example:
- function test(string $test = null) {}; // Deprecated
+ function test(?string $test = null) {}
test('PHP'); // Allowed
test(null); // Allowed
Round() Function Options
The round() function will now include additional rounding option such as PHP_ROUND_CEILING, PHP_ROUND_FLOOR, PHP_ROUND_AWAY_FROM_ZEROand PHP_ROUND_AWAY_TOWARD_ZERO
You can find the list of behavior for each options
New HTTP Methods
PHP 8.4 introduces two new functions to modernize and streamline HTTP header management in PHP scripts:
-
http_get_last_response_headers()
Retrieves headers from the last HTTP response received by the script, replacing direct access to the$http_response_header
variable. This enhances readability and reduces confusion associated with its historical usage. -
http_clear_last_response_headers()
Clears headers retrieved by the previous function, offering improved control and management of HTTP header data within an application’s lifecycle.
Summary
In summary, PHP 8.4 introduces valuable enhancements like microsecond handling, simplifying tasks and improving efficiency for developers. This update also deprecates older features, promoting better security practices. With streamlined HTTP header management, PHP 8.4 aims for improved readability in scripts. Overall, these changes mark a step forward, empowering developers with better tools for building robust PHP applications.
For a complete list of changes, refer to the official documentation:
PHP 8.4: What’s New and Changed