PHP-Versions & features
PHP 8.2 New Features :
PHP 8.1 New Features :
PHP 8.0 New Features:
// **Named Arguments**
greet(age: 25, name: "John");
// **Union Types**
function square(int|float $num): int|float {
// **Nullsafe Operator**
$avatar = $user?->getProfile()?->getAvatar();
// **Match Expression**
$result = match($grade) {
'A' => 'Excellent',
'B' => 'Good',
'C' => 'Average',
default => 'Fail',
};
// **Constructor Property Promotion**
public function __construct(public int $x, public int $y) {}
PHP 7.4 New Features:
<?php
// Typed Properties
class MyClass {
public int $counter = 0;
}
// Arrow Functions
$addOne = fn($x) => $x + 1;
$result = $addOne(5); // Result will be 6
// Null Coalescing Assignment Operator
$value ??= 'default';
// Spread Operator in Array Expression
$mergedArray = [...$array1, ...$array2];
PHP 7.3 New Features:
<?php
// Flexible Heredoc and Nowdoc Syntax
$str = <<<HTML
<div>
Hello, $name!
</div>
HTML;
// JSON_THROW_ON_ERROR
$data = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
// is_countable()
if (is_countable($array)) {
$count = count($array);
}
// Trailing Commas in Function Calls
function example(
int $a,
string $b,
) {
// function body
}
PHP 7.2 New Features:
<?php
// Object type hinting
function foo(MyClass $obj) {
// $obj must be an instance of MyClass
}
// Parameter type widening
function bar(string ...$args) {
// $args is an array of strings
}
// Object return type declarations
function createObject(): MyClass {
return new MyClass();
}
// Trailing commas in function calls
function example(
int $a,
string $b,
) {
// function body
}
PHP 7.1 New Features:
<?php
// Nullable types
function foo(?int $bar): ?int {
return $bar;
}
// Void return type
function log(string $message): void {
file_put_contents('log.txt', $message, FILE_APPEND);
}
// Iterable pseudo-type
function process(iterable $items) {
foreach ($items as $item) {
// process item
}
}
// Array destructuring assignment
[$a, $b] = [1, 2];
PHP 7.0 New Features:
<?php
// Scalar type declarations
function add(int $a, int $b): int {
return $a + $b;
}
// Null coalescing operator
$name = $_GET['name'] ?? 'Guest';
// Spaceship operator
$result = $a <=> $b;
Startup Enthusiastic || python, PHP, Node || Laravel, CodeIgniter, CakePHP, Symfony, Zend || AWS, GCP, Socket, unit-test || OOAD, Design Pattern, TDD, Agile
10 个月CFBR