PHP 8.3 is the major update from PHP 7. It is introduces many important changes and improvements. We’ve listed the main changes below including the examples.
What is the main difference between PHP 7 and PHP 8.3
1. Performance Improvement:
PHP 7: Good performance compared to its predecessors.
PHP 8.3: Enhanced performance due to JIT compiler.
Example: Complex calculations and loops run faster in PHP 8.3.
2. Named Arguments:
PHP 7: No named arguments; arguments are passed based on order.
PHP 8.3: Allows passing arguments by their names.
Example: `function foo(string $a, string $b) {}; foo(b: ‘value1’, a: ‘value2’);`
3. Attributes (Annotations):
PHP 7: No native support; uses PHPDoc comments for annotations.
PHP 8.3: Native attribute syntax.
Example: `#[Attribute] class MyClass {}`
4. Constructor Property Promotion:
PHP 7: Properties need to be declared separately.
PHP 8.3: Simplifies property declaration and assignment in constructors.
Example: `public function __construct(public string $name) {}`
5. Match Expression:
PHP 7: Uses `switch` statements.
PHP 8.3: More concise and powerful `match` expression.
Example: `$result = match($input) { ‘a’ => 1, ‘b’ => 2 };`
6. Union Types:
PHP 7: Single type declaration for function parameters and return types.
PHP 8.3: Allows multiple types for arguments and returns.
Example: `function foo(int|string $input): int|float {}`
7. Nullsafe Operator:
PHP 7: Requires checking for `null` before property or method access.
PHP 8.3: Simplifies null checks with `?->`.
Example: `$date = $booking->transaction?->date;`
8. Improvements in Error Handling and Consistency:
PHP 7: Traditional warnings and notices.
PHP 8.3: Converts many warnings and notices into Error exceptions.
Example: Code that used to emit a warning now throws an Error exception.
9. New Functions and Class Methods:
PHP 7: Limited to the set of functions available up to PHP 7.x.
PHP 8.3: Introduces new functions and class methods.
Example: New string functions like `str_contains`, `str_starts_with`, and `str_ends_with`.
10. Type System and Error Handling Enhancements:
PHP 7: Basic type system.
PHP 8.3: More robust type system and error handling.
Example: More strict type checking in function declarations and returns.
11. Deprecations and Removals:
PHP 7: Certain functions and features are standard.
PHP 8.3: Deprecates and removes some older functions/features.
Example: Functions deprecated in PHP 8.x are removed or modified.
12. Improved String Handling:
PHP 7: Standard string handling functions.
PHP 8.3: Enhanced string handling capabilities.
Example: Usage of `str_contains()` to check if a string contains a specific substring.
13. Fibers for Asynchronous Programming:
PHP 7: Lacks native support for asynchronous programming.
PHP 8.3: Introduces Fibers.
Example: `Fiber::suspend()` and `Fiber::resume()` for cooperative multitasking.
These examples illustrate how PHP 8.3 introduces more modern features, improves performance, and adds new capabilities, making it a substantial upgrade over PHP 7.