PHP, or Hypertext Preprocessor, is a very popular programming language used especially in web development.
The release of version 8 of PHP is scheduled for December 3, 2020 and some of the main innovations that it will incorporate are already known.
As it is a larger version, the changes it will introduce may be incompatible with previous versions, obtaining in return both performance improvements and very interesting features.
Next we will review what the main changes will be, not all, that we will find in PHP 8.
Union types are a collection of two or more data types. In this way we can have variables that accept being of different types. For example, I can have a number that can be used as int or as float.
New functions for strings
PHP 8 also brings a series of new functions to simplify the way we work with the string type.
Surely the most interesting of them is str_contains, which solves us having to use the strpos function to check if one string contains another.
In addition to this function, PHP 8 incorporates two more:
- str_starts_with to check if a string starts with a certain string.
- str_ends_with to check if a string ends with a certain string.
Catch without any variable
Another improvement that PHP 8 brings is the possibility of specifying try / catch blocks without the catch having to declare the variable that collects the exception, since sometimes it is not used at all, adding visual noise to the code or even being signaled as a warning by the editor.
Attributes or annotations allow us to add metadata to our classes, something that is surely familiar to you if you work with frameworks like Symfony or Laravel.
As proposed in the RFC, from PHP 8 we will be able to specify attributes / annotations without having to resort to comment blocks.
To do this, the first thing we will do is create an attribute as if it were a class and using the <<PhpAttribute>> attribute so that it can be identified as such.