Research Documentation 05 Php8

Research Documentation 05 Php8

05 Php8

Feature 1: str_contains Function

One great function that PHP 8 added was the str_contains function. This function can be used instead of strpos to find out if a string is found in another string. By using this function you can say str_contains(string $originalstring, string $searchstring) and it returns true if found and false if not found. It is much more straighforward and easy to read the code than with strpos.

Feature 2: Named Arguments

Named arguments is a new feature that simplifies calling methods and functions. It allows you to name the variables passed to the method/function. By doing this, you can then send the variables out of order by referencing their names, or not send them at all.

Feature 3: Union Types

Union types allow a variable sent to a function to be a union of different types. The example showin on the How to Geek website was: public function foo(string|int $bar) : int|float; This use of a union allows the variable sent into the function to be either an integer or a float and allows the return variable to be an integer or a float.

Summary

It was interesting to see the various new things added in PHP 8. Some of them were way above my skill level (or ability to fully understand what it could do or how it even works!). But the three I picked were ones that I could see being very useful for what I know already. They give simplicity to the coding and allow for more flexibility in how functions are called.

Sources: