class User
{
/ @var int */
public $id;
/** @var string */
public $name;
/** @var string|null */
public $email;
public function __construct(
int $id,
string $name,
?string $email = null
) {
$this->id = $id;
$this->name = $name;
$this->email = $email;
}
}
$user = new User(1, 'Alice');class User
{
public int $id;
public string $name;
public ?string $email;
public function __construct(
int $id,
string $name,
?string $email = null
) {
$this->id =$id;
$this->name =$name;
$this->email =$email;
}
}
$user = new User(1, 'Alice');$factor = 10;
$numbers = [1, 2, 3, 4, 5];
$multiplied = array_map(
function (int $n) use ($factor): int {
return $n * $factor;
},
$numbers
);$factor = 10;
$numbers = [1, 2, 3, 4, 5];
$multiplied = array_map(
fn(int $n): int => $n * $factor,
$numbers
);$user = ['role' => 'editor'];
// Assign default if key is missing or null
$user['role'] = $user['role'] ?? 'guest';
$user['status'] = $user['status'] ?? 'active';
$config = [];
if (!isset($config['timeout'])) {
$config['timeout'] = 30;
}$user = ['role' => 'editor'];
// Assign default if key is missing or null
$user['role'] ??= 'guest';
$user['status'] ??= 'active';
$config = [];
$config['timeout'] ??= 30;$fruits = ['apple', 'banana'];$vegetables = ['carrot', 'pea'];
$groceries = array_merge(
['bread'],
$fruits, $vegetables,
['milk']
);$fruits = ['apple', 'banana'];
$vegetables = ['carrot', 'pea'];
$groceries = [
'bread',
...$fruits,
...$vegetables,
'milk',
];