Introduction
Initially known as the Personal Home Page, PHP or Hypertext Preprocessor is an open-source programming language used for server-side scripting of web applications. It offers extensive libraries for common functions to cut down the code writing time. Thanks to its user-friendly attributes and extensive scope of capabilities, PHP quickly became popular, so much so that more than 75% of websites use it for server-side programming.
One of the most significant advantages of PHP is that it receives regular updates to fix bugs and introduce new features constantly. That’s why developers all around the world eagerly wait for the latest versions to improve and reinforce their web applications. The newest version of PHP, Version 8.2, was launched in December 2022.
So in this blog, we’ll discuss what's new in PHP 8.2 , its newly introduced features, deprecated attributes, and other changes. But first, let’s see why PHP is so popular.
What Are The Advantages of PHP?
Here are a few advantages of PHP that make it incredibly popular:
Enhanced Page Loading Speed: One of the most significant advantages of PHP is the enhanced page loading speed it offers. It is especially good for the SEO aspect of the web pages, as page load speed and performance is an essential factor for achieving good ranks on the SERPs.
Cost-Effective Platform: PHP, by default, is free to use, which means you’ll have more money in your budget for the development of your web application. Moreover, almost every tool or software used in combination with PHP is free as well.
Open-Source: Another pro of PHP is that it is an open-source platform. That means it enables the developers to edit their structure and use it efficiently in their future projects.
Flexibility: PHP is flexible enough to be combined with other programming languages. You can also use it across operating systems, including macOS, Windows, LINUX, etc., for coding.
Variety of Databases: With PHP, you can connect to almost any database, like MySQL, MS-SQL, PostgreSQL, mSQL, SQLite, and more. So you have a choice of selecting the most suitable database to develop the best web application.
Ample Documentation: PHP is quite easy to use, more so with the extent of documentation available on the internet. From the extensive guides and tutorial documents to thousands of videos on Youtube, even beginners without any experience in coding can easily develop the server-side of web applications.
Although there are several other advantages of PHP, these might be enough to help you understand why it is the most widely used server-side platform around the world.
PHP Version 8.2
Launched in December 2022, Version 8.2 of PHP introduced new features, eased the development process, and fixed the bugs from the previous versions.
It also removed the deprecated features that were older and less used in the process. If your web application is based on an older version of PHP, it’s imperative you upgrade it to the latest one. It will help you add more features to your application and ensure more security.
So let’s take a look at all the latest introductions and deprecations in the newest version of PHP.
Latest Features of PHP 8.2
The newly-introduced features in PHP 8.2 include:
True, False, and Null as Standalone Types
In the previous version of PHP, you could easily declare a particular type as the unification of two or more individual types. Although false and null were usable as types, you couldn’t use them as standalone types. But now, with PHP 8.2, you can use them as standalone types, making PHP more descriptive with the help of more precisely declared parameters, returns, and property types. Here is an example of false being used as a standalone type:
function alwaysFalse(): false
{
return false;
}
Disjunctive Normal Form Types
PHP 8.2 lets you use the new Disjunctive Normal Types feature to organize the boolean expressions. It consists of a boolean OR of ANDs. Here’s an example of DNF types:
// Accepts an object that implements both A and B,
// OR an object that implements D.
(A&B)|D
// Accepts an object that implements C,
// OR a child of X that also implements D,
// OR null.
C|(X&D)|null
// Accepts an object that implements all three of A, B, and D,
// OR an int,
// OR null.
(A&B&D)|int|null
Constants in Traits
PHP 8.2 also introduced a way to reuse the codes in Traits by defining the properties and methods, which is excellent for code reuse across classes. This version lets you define constants in Traits.
`Here is an RFC proposal for an example:
trait Foo
{
public const CONSTANT = 1;
}
class Bar
{
use Foo;
}
var_dump(Bar::CONSTANT); // 1
var_dump(Foo::CONSTANT); // Error`
Readonly Classes
Although the previous version had a readonly feature for class properties, the latest version of PHP has also introduced support for the entire class to be defined as readonly. Declaring a class readonly means all properties of that class will be readonly as well.
For example:
readonly class MyClass
{
public string $myValue,
public int $myOtherValue
public string $myAnotherValue
public int $myYetAnotherValue
}
In this piece of code, you can see the whole class is defined as readonly. So all properties and elements are in the readonly mode. This version of PHP also lets you define the final or abstract classes as readonly. Other than that, you can define a class without any properties as , readonly which prevents the main class from having dynamic properties while declaring the child classes’ properties as readonly explicitly.
_
Note: Do not define a readonly class without typed properties, or it may result in a fatal error. Moreover, you can’t define certain PHP features, readonly including Traits, Enums, and Interfaces. Trying to do so will result in a Parse error.
_
Redacting The Support For Sensitive Parameter Values
With this feature, you can track the call stack at any point in a program. You can also debug your application and see why there are errors. But there may be certain stacks in your program containing sensitive information. With this version 8.2, you can easily mask this information via a feature called, which will help prevent this sensitive info from being logged in case of any error.
Here’s an example of the SensitiveParameter feature:
`function passwords(
$publicpassword,
[\SensitiveParameter] $secretpassword
) {
throw new \Exception('Error');
}
passwords('publicpassword', 'secretpassword');`
PHP has introduced these features in the latest version to simplify and enhance the developmental process of web applications.
New Functions and Classes of PHP 8.2
PHP 8.2 comes with a number of new functions and classes, in addition to the major new features:
New mysqli_execute_query Function and mysqli::execute_query Method
This new version of PHP introduced a better and smoother way to take care of the precomputed MySQLi queries. Through the mysqli::execute_query method and mysqli_execute_query function, you can prepare, define and execute the necessary queries within the same function.
The new function introduced in PHP 8.2 is combined using the mysqli_prepare(), mysqli_execute(), and mysqli_stmt_get_result() functions, and with it, the MySQLi will be executed from within.
Here is an RFC proposal for an example:
foreach ($db->execute_query('SELECT * FROM user WHERE name LIKE ? AND type_id IN (?, ?)', [$name, $type1, $type2]) as $row) {
print_r($row);
}
Parse INI quantity values: ini_parse_quantity
The ini_parse_quantity function reads any data size that PHP INI values can recognize, such as 56K, 256M, or 1G, and returns the size of the data in bytes.
ini_parse_quantity('256M'); // 268435456
Maintain an existing Curl connection: curl_upkeep
The curl_upkeep function in the PHP 8.2 Curl extension tells the underlying Curl library to do what it needs to do to keep a Curl connection alive. The most common way to use this function is to call curl _upkeep periodically to keep an HTTP persistent connection (Keep-Alive) alive.
Get the Length of the Cipher Key: openssl_cipher_key_length
In PHP 8.2 OpenSSL, there is a new function called openssl_cipher_key_length that returns the required length of the key (in bytes) for any OpenSSL cypher.
This function gets rid of the need for OpenSSL cypher operations to hard-code the length of the key they need.
openssl_cipher_key_length("CHACHA20-POLY1305"); // 32
openssl_cipher_key_length("AES-128-GCM"); // 16
openssl_cipher_key_length("AES-256-GCM"); // 32
*memory_reset_peak_usage – Reset recorded peak memory usage
*
memory_reset_peak_usage is a new function in PHP 8.2 that resets the peak memory usage reported by the memory_reset_peak_usage function.
This can be useful in applications that require recording the peak memory usage of each invocation after invoking or iterating an action numerous times. memory_reset_peak_usage returns the absolute peak memory usage throughout the entire run without the opportunity to reset the memory consumption with the new memory_reset_peak_usage function.
Deprecated Features of PHP 8.2
Here are the features that existed in the previous versions of PHP but are not anymore:
*Mbstring functions for QPrint/HTML Entities/Base64/Uuencode
*
Mbstring or multi-byte string functions are used to convert to and from the different coding standards like ISO-8859-1 and UTF-8/16/32, along with Quoted-Printable, HTML entities, Base64, and Uuencode. But PHP 8.2 has deprecated the mbstring extensions to these labeled encodings.
*${var} String Interpolation
*
With the earlier versions of PHP, you can replace the variable values within a string literal with double quotes. For example:
`“{$myname}” – braces outside the variable.
”${expr}” – variable variables equivalent to using (string) ${expr}
“$myname” – directly embedding variables.
“${myname}” – braces after the dollar sign.`
While ensuring the same functionality, the last two syntaxes are quite complex. That’s why they will be deprecated in PHP 8.2.
#utf8_encode() and utf8_decode()
These functions can be used to convert to and from between UTF-8 and ISO-8859-1 encoding standards. Although these functions will be deprecated in this version, they will be removed altogether in the PHP 9.0.
Partially-Supported Callables
The partially-supported callables will be deprecated by PHP 8.2, so they won’t work with the $callable() patterns.
Here are a few of the deprecated callables:
$callable = "parent::method";
$callable = ["self", "method"];
$callable = ["static", "method"];
$callable = [new MyClass(), "MyOtherClass::myMethod"];
$callable = "self::method";
$callable = "static::method";
$callable = ["parent", "method"];
$callable = ["MyClass", "MyParentClass::myMethod"];
To prevent the deprecation, you can convert all of the parent, self, and static keywords to their corresponding class names.
These deprecated features are usually not needed in web application development of the web applications without the impact on the features.
**Upgrade your PHP Version**
Like any other popular web framework, people can have some issues with PHP during application development. But it receives regular updates, the newest one of which is Version 8.2. Not only has this update introduced new features to improve the functionality and security of the application, but it also eases its development process.
So if you are not trained or experienced in programming, it is very important you can take help from hiring expert PHP developers to upgrade your PHP to its latest version.
Top comments (0)