PHP 8.0 comes to web servers and hostings
The big change PHP 8.0 comes to web servers
PHP versions change frequently, and we usually don't notice that when you log in to the web hosting self-service, you are notified of the outdated version and asked to upgrade.
Life goes on in the same way with the newer version. But this time, the transition is bigger and more important. Kristjan Aas, the web developer of the Web Doctor, explains the changes in more detail to the expert .
PHP 8.0 is a major upgrade that will bring many changes to the data type system, syntax, error handling, strings, object-oriented programming, and more. Here is an overview of all the important changes that experts and could be aware of.
Stricter type control for arithmetic and bit based operators
Before PHP 8, it was possible to implement bit-based operators on objects, resources, and arrays. However, as of PHP 8, this is no longer possible and results in a TypeError error message . This usually means that the value is not of the expected type.
The default error information level changes
As of PHP 8, the error information level is E_ALL, which means that many such error messages may be visible that were previously ignored.
Reclassified alerts and notifications become errors
Many minor errors that previously only triggered warnings and notifications have now been converted to errors. The following warnings were changed:
- Undefined variable: Error exception instead of message;
- Undefined array index: warning instead of notification;
- Division by zero: DivisionByZeroError exception instead of warning;
- Attempt to increment / decrement property '% s' of non-object: Error exception instead of warning;
- Attempt to modify property '% s' of non-object: Error exception instead of warning;
- Attempt to assign property '% s' of non-object: Error exception instead of warning;
- Creating default object from empty value: Error exception instead of warning;
- Trying to get property '% s' of non-object: warning instead of notification;
- Undefined property:% s :: $% s: warning instead of notification;
- Cannot add element to the array as the next element is already occupied: Error exception instead of warning;
- Cannot unset offset in a non-array variable: Error exception instead of warning;
- Cannot use a scalar value as an array: (Error Error) instead of a warning;
- Only arrays and Traversables can be unpacked: TypeError exception (TypeError Erind) instead of warning;
- Invalid argument supplied for foreach (): TypeError exception instead of warning;
- Illegal offset type: TypeError exception instead of Warning;
- Illegal offset type in isset or empty: (TypeError Erind) instead of warning;
- Illegal offset type in unset: (TypeError Erind) instead of warning;
- Array to string conversion: warning instead of notification;
- Resource ID #% d used as offset, casting to integer (% d): warning instead of notification;
- String offset cast occurred : warning instead of notification;
- Uninitialized string offset:% d: warning instead of notification;
- Cannot assign an empty string to a string offset: Error exception instead of warning;
- Supplied resource is not a valid stream resource: (TypeError Erind) instead of a warning;
Attributes in PHP 8
PHP 8 allows us to use attributes that allow us to declare metadata for functions, classes, variables, and parameters.
Constructor specimen fields
The updated PHP 8 uses a new syntax for declaring instance fields directly from the class constructor.
What is a Null-safe operator?
A zero-safe operator provides safety when linking methods or variables when the return value or variable value may be zero.
? -> The null-safe operator interrupts the rest of the expression when it encounters a null value and returns zero immediately without causing an error.
Union Types (or union-type)
A Union type is a collection of two or more data types that can be used as the value of an input and output parameter for a variable or function.
In the example, the value of the variable $ number can be int (integer) and float (floating-point number) at the same time.
Named arguments ( Named arguments or parameters)
Named arguments or parameters allow functions to be given input parameters using the variables' own names. Thus, the input parameters of the function do not depend on the order of the parameters.
The match expression is a descendant of Switch
A match expression can be called a descendant of Switch . The match expression returns a value and does not require a separate break command.
Non-Capturing Catches (ie mittepüütavad outliers)
In the past, you had to keep this variable to catch a difference, whether you used this variable or not. However, in the case of a non-catchable difference, you can omit these variables.
If in the past the exception had to be tried like this
then starting with PHP 8 it is possible to do this as follows
Brand new str_contains () function
If previously you had to use the str_pos () function to find out if a string contains another string, a new function called str_contains () has been available since PHP 8.
If this was the case in the past
Then starting with PHP 8, it's possible to do it a little easier with a new feature
The new str_starts_with () and str_ends_with () functions also need to be mentioned.
These two string-to-string search functions simplify the search as follows:
- str_starts_with () - can be used to check if a string starts with a certain string
- str_ends_with () - can be used to check if a string ends with a certain string
Disapproved has now been removed
It is important to know that everything that was declared undesirable during PHP 7.x development has now been removed in PHP 8:
- NOT announced PHP 7.4 -S
- NOT announced PHP 7.3 -S
- NOTs announced PHP 7.2 -S
- NOT announced PHP 7.1 -S