20 New PHP Features You Must Know in 2023

20 new php feature in 2023

PHP is always evolving and it is important to stay up to date with its latest features and improvements. This post introduces 20 PHP features you need to know by 2023, each illustrated with a code example.

1) str_contains(): Checks if a string is contained within another string.

$sentence = "I'm pars.team and love Hive Blockchain. woooo!";
$word = "Hive";
if (str_contains($sentence, $word)) {
echo "Pummm. I find it!";
}

2) str_starts_with(): Checks if a string starts with a specified substring.

$sentence = " Hive is my Love!";
if (str_starts_with($sentence, "")) {
echo "The sentence starts with a rocket emoji!";
}

3) str_ends_with(): Checks if a string ends with a specified substring.

$sentence = "I'm pars.team! ";
if (str_ends_with($sentence, "")) {
echo "The sentence ends with a sun emoji!";
}

4) get_debug_type(): Gets the type of the variable.

$num = 42;
echo get_debug_type($num); // "integer"

5) get_resource_id(): Returns the unique ID of the data.

$file = fopen('hivebot.txt', 'r');
echo get_resource_id($file); // e.g., "7"

6) fdiv(): A division function that does not support division by zero.

$result = fdiv(10, 0); // INF

7) preg_last_error_msg(): Returns a human-readable message and the last error of the PCRE regex execution.

preg_match('/(/', '');
echo preg_last_error_msg(); // "missing )"

8) array_key_first(): Retrieves the first key of an array.

$array = [''=>'Hive', ''=>'HBD', ''=>'BEE'];
echo array_key_first($array); // ""

9) array_key_last(): Retrieves the last key of an array.

$array = [''=>'pars.team', ''=>'ocdb', ''=>'ecency'];
echo array_key_last($array); // ""

10) ErrorException::getSeverity(): Receives the importance and severity of the error.

try {
trigger_error("Custom error", E_USER_WARNING);
} catch (ErrorException $e) {
echo $e->getSeverity(); // 512
}

11) Filter functions: PHP 8 has introduced several new filter functions. Here is an example of using filter_var with FILTER_VALIDATE_BOOL:

var_dump(filter_var('yes', FILTER_VALIDATE_BOOL)); // bool(true)

12) WeakMap class: A new class that holds references to objects, which does not prevent objects from being garbage collected.

$weakmap = new WeakMap();
$obj = new stdClass();
$weakmap[$obj] = 'Hello, world!';

13) Amount of objects: PHP 8 introduced Constructor Property Promotion, a new syntax for constructing object values.

class Money {
public function __construct(
public int $amount,
public string $currency
) {}
}
$tenDollars = new Money(10, 'USD');

14) Match phrase: Match is an expression like switch.

echo match (1) {
0 => '',
1 => '',
default => '',
};

15) Nullsafe operator: This new operator (?->) allows checking for nullity when accessing properties or methods.

class User {
public function getAddress(): ?Address {
// returns Address or null
}
}
$user = new User(); $country = $user?->getAddress()?->country; // no error if getAddress() returns null

16) Named arguments: This feature allows you to pass values to a function by specifying the value name.

new Money(amount: 10, currency: 'USD');

17) Attributes: Also known as annotations in other programming languages.

#[Attribute]
class ExampleAttribute {}
#[ExampleAttribute] class ExampleClass {}

18) Improved Constructor feature: This property allows the combination of class properties and constructor in a single definition.

class Money {
public function __construct(
public int $amount,
public string $currency
) {}
}

19) Union types: This feature allows the definition of type, which can have one of several types.

function print_id(int|string $id): void {
echo 'ID: ' . $id;
}

20) Compile Just In Time (JIT): PHP 8 introduces two JIT compilation engines, Tracing JIT and Function JIT.

JIT compilation is not a feature that can be directly demonstrated with a code snippet, but it is an important development in PHP 8 that can significantly improve performance.

As a result, PHP is a constantly evolving language with many exciting new features and improvements. Whether you're an experienced PHP developer or a newbie, it's worth taking the time to learn about these new features and use them in your code. Stay curious, keep learning and have fun coding!


[HIVE : @pars.team]



0
0
0.000
5 comments
avatar

Congratulations @pars.team! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You received more than 50 upvotes.
Your next target is to reach 100 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out our last posts:

LEO Power Up Day - November 15, 2023
0
0
0.000