1843
A Sneak Peek into Latest PHP 8.3 Update: Release Date, New Features, & Improvements
24 Mar, 2023
7 min read
1843
24 Mar, 2023
7 min read
Table of Content
PHP 8 was released on November 26, 2020, with many interesting features like match expression, attributes, union types, null safe operator, JIT, named arguments, etc. All of these features add more to the already popular programming language.
Now, with the upcoming release of PHP 8.3, you can expect more additions to it. The update is expected to release on November 23, 2023, after it has passed 3 alpha and beta releases.
So, what are some new features to expect from this release? In this blog post, I have enlisted all of it. But before we do so, let’s give you a little background knowledge about PHP server and PHP as a programming language.
PHP Vs PHP 8 Popularity
PHP 8 Sub-version Usage Statistics
Are you wondering when it will be released completely and will be available for public use? Here’s the release schedule for you to check!
Release Date | Type |
---|---|
June 8, 2023 | Alpha 1 |
June 22, 2023 | Alpha 2 |
July 6, 2023 | Alpha 3 |
July 18, 2023 | Feature freeze |
July 20, 2023 | Beta 1 |
August 03, 2023 | Beta 2 |
August 17, 2023 | Beta 3 |
August 31, 2023 | RC 1 |
September 14, 2023 | RC 2 |
September 28, 2023 | RC 3 |
October 12, 2023 | RC 4 |
October 26, 2023 | RC 5 |
November 9, 2023 | RC 6 |
November 23, 2023 | GA |
Curious to know what PHP 8.3 has in store for developers and businesses that depend on it? Let’s dive right into it!
A JSON string consists of a specified data format. It doesn’t include any methods but only properties. Writing a JSON string needs you to write a string in double quotes. If you write it in single quotes, it isn’t valid.
Until now, if you wanted to validate a JSON string, you would have to use the function json_decode(). You would have to write something like this:
$json = '{"name": "Alicia Joe"}';
$data = json_decode($json); // function to validate JSON
if (json_last_error() === JSON_ERROR_NONE) {
// Valid JSON
} else {
// Invalid JSON
}
With the release of PHP 8.3, you can now use the method json_validate( ) as follows:
$json = '{"name": "Alicia Joe"}';
$valid = json_validate($json);
if ($valid) {
// Valid JSON
} else {
// Invalid JSON
}
If you compare the two pieces of code, you can see that using the json_validate( ) method is much more easy and simple. If it is a valid JSON string, it returns true. Otherwise, it returns a False.
Here, you also don’t have to be dependent on the json_last_error() function. Check out the signature of the json_validate() function mentioned below:
json_validate(string $json, int $depth = 512, int $flags = 0): bool
Here, $json is the JSON string that needs to be validated, $depth is the nesting depth (maximum) of the decoding structure, $flags is the bitcode of decode flags.
Catching errors and exceptions will be much easier with the release of PHP 8.3. Up until now, you would get an E_WARNING or E_NOTICE in case of an error or it would simply throw arbitrary errors and exceptions.
You had to write something like this for error handling:
try {
set_error_handler(static function ($severity, $message, $file, $line) {
throw new \ErrorException($message, 0, $severity, $file, $line);
});
$result = unserialize($serialized);
} catch (\Throwable $e) {
// Unserialization failed. Catch block is optional in case the error shouldn’t be handled.
} finally {
restore_error_handler();
}
var_dump($result);
Here, you needed to set an error handler, catch exceptions, and restore the error handler. You can’t even guarantee that the restore error handler option will function seamlessly in case of exception. It can be a lot of hassle.
However, the new PHP 8.3 comes with an improved unserialize( ) function. Thus, if your unserialize( ) method fails, it will throw an UnserializationFailedException. The E_WARNING or E_NOTICE will be converted into exceptions , wrapped in an instance of UnserializationFailedException. It makes catching and handling errors much easier.
So, instead of writing the aforementioned snippet of code, you can use the unserialize method like this:
try {
$result = unserialize('B:2:"jon";');
var_dump($result);
// Do something with the $result.
} catch (\UnserializationFailureException $e) {
// unserialization failed.
}
Several methods are proposed for the class \Random\Randomizer. With the help of these functions, you will have better opportunities of creating random values. Here, look at some of these functions added in the PHP last version.
Random string generation
This method can be used on multiple occasions. For example, you can generate a string of desired length from randomly chosen bytes of a string. Consider the following code for better understanding:
$randomizer = new \Random\Randomizer();
$randomizer->getBytesFromString('abcdef', 3); // 'bda'
Here, we have given the length as 3 and the function has randomly selected three bytes from the provided string.
Creating multi factor authentication code
$randomizer = new \Random\Randomizer();
var_dump(
implode('-', str_split($randomizer->getBytesFromString('0123456789', 20), 5))
);
// string(23) "09292-16502-59535-88886"
Creating a subdomain name randomly
$randomizer = new \Random\Randomizer();
var_dump(
$randomizer->getBytesFromString('abcdefghijklmnopqrstuvwxyz', 10) . '.example.com'
);
// string(20) "jxqzjxqzjx.example.com"
Creating a DNA sequence
$randomizer = new \Random\Randomizer();
var_dump(
$randomizer->getBytesFromString('ATGC', 30)
);
// string(30) "TCTGCTGCTGCTGCTGCTGCTGCTGCTGCT"
public function getFloat(
float $min,
float $max,
IntervalBoundary $boundary = IntervalBoundary::ClosedOpen
): float {}
Getting a random float number
This function is utilized to randomly get a float number between any two provided numbers. Here, check out the code for your reference:
$randomizer = new \Random\Randomizer();
$randomizer->getFloat(0, 1); // 0.123456789
Generating latitude and longitude randomly
$randomizer = new \Random\Randomizer();
var_dump(
$randomizer->getFloat(-90, 90, \Random\IntervalBoundary::ClosedClosed),
$randomizer->getFloat(-180, 180, \Random\IntervalBoundary::OpenClosed)
);
// Lat: float(-45.123456789)
// Long: float(123.123456789)
Syntax:
public function nextFloat(): float {}
Generating a float number between the numbers 0 and 1 randomly
$randomizer = new \Random\Randomizer();
$randomizer->nextFloat(); // 0.123456789
$randomizer->nextFloat(); // 0.987654321
Of course, it might look similar to the getfloat() method, but it is strictly used for generating a random float number between 0 and 1.
Simulating a coin flip
$randomizer = new \Random\Randomizer();
var_dump(
$randomizer->nextFloat() > 0.5 ? 'Heads' : 'Tails'
);
// string(5) "Tails"
Simulating a dice roll
$randomizer = new \Random\Randomizer();
var_dump(
(int) ($randomizer->nextFloat() * 6) + 1
);
// int(3)
Up until now, PHP did support dynamic or computed functions and properties.
//Calling variables dynamically
$baz = 'foo';
$$baz = 'bar';
^
// $foo
// Calling class functions dynamically
class Foo
{
public function hello()
{
echo 'Hello world!';
}
}
$dynamicMethod = 'hello';
$a = new Foo();
$a->{$dynamicMethod}(); //prints 'Hello!'
However, calling class constants dynamically was not possible yet. But, not anymore! In PHP 8.3, you can expect this feature to be present.
class Foo
{
public const BAR = 'bar';
}
$dynamicConstant = 'BAR';
echo Foo::{$dynamicConstant};
//prints 'bar'
In case you are trying to access a constant that does not exist, it will throw an error like mentioned below.
class Foo {}
$bar = 'BAR';
echo Foo::{$bar};
// Error: Undefined constant Foo::BAR
With PHP 8.3, some of the date/time exceptions are expected to improve sufficiently. If you see, most of these exceptions are very generic and don’t provide much information about these errors, making it quite difficult to catch them.
But in the PHP 8.3 update, this issue will be addressed as more specific exceptions will be present. For instance, these errors will be available.
Exceptions
While these are some of the most notable features and updates you will get to enjoy with the new update, there are some backward compatibility issues as well that you have to note.
The new PHP 8.3 version is expected to bring a lot of new features and improvements to the already powerful scripting language. Such as Randomizer additions, json() validate, unserialize() error handling. But more than that, it is going to be an exceptional update for coders and testers who have to work their brains in understanding generic errors and exceptions.
With the inclusion of specified error and exception suggestions, you will be able to catch errors and rectify the code seamlessly. Want to know how you can leverage this update to code better software products? Our developers can help you with that.
With our 16+ years of experience and rich industry skills, we are well aware of the ins and outs of PHP development. Let’s connect to discuss more!
Development
Laravel
127
By Devik Gondaliya
22 Aug, 2024
Development
Devops
125
By Biztech
20 Aug, 2024
Javascript
248
By Devik Gondaliya
13 Aug, 2024