PHP 8 : What’s new?

PHP8 : What’s new?

Better performance, better syntax, improved type safety.

HouseOfCoder
3 min readMay 6, 2021

--

PHP 8 was released on 26 Nov 2020. Since it’s a major release it came with lots of new features and optimizations, I’ve listed few of them below.

Named arguments

Named arguments allow passing arguments to a function based on the parameter name, rather than the parameter position. This makes the meaning of the argument self-documenting, makes the arguments order-independent, and allows skipping default values arbitrarily.

PHP8 : Named arguments

If you notice in above example I did not follow the parameter order while calling details() function.
It is possible to combine named arguments with normal, positional arguments and it is also possible to specify only some of the optional arguments of a function, regardless of their order. More on Named arguments

Constructor Promotion

When a constructor argument includes a visibility modifier, PHP will interpret it as both an object property and a constructor argument, and assign the argument value to the property. The constructor body may then be empty or may contain other statements. More on Constructor Promotion

PHP8 : Constructor Promotion

Union types

Union types are available as of PHP 8.0.0.
A union type declaration accepts values of multiple different types, rather than a single one. Union type declarations that are validated at runtime.

PHP8 : Union types

The null type is supported as part of unions, such that T1|T2|null can be used to create a nullable union. The false literal type is supported as part of unions. More on Union types

Match expression

The match expression is similar to a switch statement but has some key differences:

  • A match arm compares values strictly (===) instead of loosely as the switch statement does.
  • A match expression returns a value.
  • Match arms do not fall-through to later cases the way switch statements do.
  • A match expression must be exhaustive.

More on Match expression

Nullsafe operator

Instead of null check conditions, you can now use a chain of calls with the new nullsafe operator.

PHP8 : Nullsafe operator (?->)

When the evaluation of one element in the chain fails, the execution of the entire chain aborts and the entire chain evaluates to null. More on Nullsafe operator

Saner string to number comparisons

When comparing to a numeric string, PHP 8 uses a number comparison. Otherwise, it converts the number to a string and uses a string comparison.

PHP8 : Saner string to number comparisons

More on Saner string to number comparisons

If you want to know all the features which were added in PHP8, Please visit PHP8 release note and PHP8 Change log.

If you like this article make sure to clapp-clapp 👏 and follow me on Medium for more such articles. Suggestions in the comments are always welcome :)

As content generation is not a easy process soooo, I wouldn’t mind if you gift me Ko-fi to motivate and boost my confidence :)

--

--

HouseOfCoder
HouseOfCoder

Written by HouseOfCoder

Web developer by profession. Crafting code and contemplative thoughts. Join me on a journey of tech, life, and mindfulness.

No responses yet