4177
PHP 8.3 Update: New Features & Improvements
7 min read
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
| 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!
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.
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.
}
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 RandomRandomizer();
$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 RandomRandomizer();
var_dump(
implode(’-’, str_split($randomizer->getBytesFromString(‘0123456789’, 20), 5))
);
// string(23) “09292-16502-59535-88886”
Creating a subdomain name randomly
$randomizer = new RandomRandomizer();
var_dump(
$randomizer->getBytesFromString(‘abcdefghijklmnopqrstuvwxyz’, 10) . ‘.example.com’
);
// string(20) “jxqzjxqzjx.example.com”
Creating a DNA sequence
$randomizer = new RandomRandomizer();
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 RandomRandomizer();
$randomizer->getFloat(0, 1); // 0.123456789
Generating latitude and longitude randomly
$randomizer = new RandomRandomizer();
var_dump(
$randomizer->getFloat(-90, 90, RandomIntervalBoundary::ClosedClosed),
$randomizer->getFloat(-180, 180, RandomIntervalBoundary::OpenClosed)
);
// Lat: float(-45.123456789)
// Long: float(123.123456789)
public function nextFloat(): float {}
Generating a float number between the numbers 0 and 1 randomly
$randomizer = new RandomRandomizer();
$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 RandomRandomizer();
var_dump(
$randomizer->nextFloat() > 0.5 ? ‘Heads’ : ‘Tails’
);
// string(5) “Tails”
Simulating a dice roll
$randomizer = new RandomRandomizer();
var_dump(
(int) ($randomizer->nextFloat() * 6) + 1
);
// int(3)
//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
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.
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!
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
| 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!
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.
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.
}
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 RandomRandomizer();
$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 RandomRandomizer();
var_dump(
implode(’-’, str_split($randomizer->getBytesFromString(‘0123456789’, 20), 5))
);
// string(23) “09292-16502-59535-88886”
Creating a subdomain name randomly
$randomizer = new RandomRandomizer();
var_dump(
$randomizer->getBytesFromString(‘abcdefghijklmnopqrstuvwxyz’, 10) . ‘.example.com’
);
// string(20) “jxqzjxqzjx.example.com”
Creating a DNA sequence
$randomizer = new RandomRandomizer();
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 RandomRandomizer();
$randomizer->getFloat(0, 1); // 0.123456789
Generating latitude and longitude randomly
$randomizer = new RandomRandomizer();
var_dump(
$randomizer->getFloat(-90, 90, RandomIntervalBoundary::ClosedClosed),
$randomizer->getFloat(-180, 180, RandomIntervalBoundary::OpenClosed)
);
// Lat: float(-45.123456789)
// Long: float(123.123456789)
public function nextFloat(): float {}
Generating a float number between the numbers 0 and 1 randomly
$randomizer = new RandomRandomizer();
$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 RandomRandomizer();
var_dump(
$randomizer->nextFloat() > 0.5 ? ‘Heads’ : ‘Tails’
);
// string(5) “Tails”
Simulating a dice roll
$randomizer = new RandomRandomizer();
var_dump(
(int) ($randomizer->nextFloat() * 6) + 1
);
// int(3)
//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
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.
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!
Artificial Intelligence (AI)
Development
10811
By Devik Gondaliya
Development
ERP
2796
By Devik Gondaliya
Artificial Intelligence (AI)
Development
52808
By Devik Gondaliya
You are at the right place.
Projects Completed
Technical Experts
Happy Clients
Years of Experience
Book a free consultation call with us
By submitting this form, you agree to our Terms of Use and Privacy Policy. All information provided will be kept strictly confidential.