What's New in PHP 8.1 and 8.2 - Derick Rethans

Php

Learn about the latest features and improvements in PHP 8.1 and 8.2, including named arguments, enums, type inference, and more, in this comprehensive conference talk.

Key takeaways
  • Named arguments are coming to PHP, allowing for more explicit type declaration and better code readability. Enums in PHP 8.1 are now backed by strings or integers, and can be used to define a set of named values. The bool type no longer includes int, and is now a distinct type that is only true/false. Discord responses will be available as an optional parameter in debug backtraces. PHP’s rand() function has been updated to use a pseudo-random number generator, and a new random_bytes() function has been added. The shuffle() function can now be used to scramble array elements. Property promotion in PHP 8.2 is enabled by default, allowing for more implicit type conversions. Type inference in PHP 8.2 will automatically infer the types of variables based on their initial assignments. Enums can be used with the value property to access their underlying values. The match statement is a new keyword in PHP, similar to a switch statement. Union types can combine multiple types into a single type declaration. Intersection types can combine multiple union types into a single type declaration. Named arguments can be used to specify default values for function arguments. The closedir function no longer returns the directory path, and instead returns an empty string. The dir() function will now correctly return the directory path. Basic types in PHP 8.2 will be further unified, with fewer implicit conversions. Set types will be added to PHP, allowing for more explicit type declaration. Sensitive parameters will be added to PHP, allowing for more secure debugging. The Parcelable type is a new type in PHP, which allows for more explicit type declaration. The min and max functions will now work correctly with arrays. The glob function will now correctly match directories. The move_uploaded_file function will now correctly handle move operations. The shell_exec function will now correctly handle shell commands.