- Scalar type declarations:
function f(int ...$args)
(bool, int, float and string, in addition to array, callable, self, parent and interface/class name). - Return type declarations:
function f(): bool
. - Constant arrays using define():
define('MY_CONSTANT', [ ... ])
. - Integer division with intdiv():
var_dump(intdiv(8, 3)); // int(3)
. - Spaceship operator:
$a <=> $b
→ {-1, 0, 1} (less, equal, bigger). - Null coalescing operator:
// $username = isset($_GET['user']) && !is_null($_GET['user']) ? $_GET['user'] : 'nobody'; $username = $_GET['user'] ?? 'nobody'; $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
- Group use declarations:
use some\namespace\{ClassA, ClassB, ClassC as C}
. - Generator return expressions.
- Generator delegation.
- Anonymous classes.
- iterable pseudo-type:
function f(iterable $i)
(accepts arrays and Traversable's). - Nullable types:
function f(?string $arg): ?int
. - Void functions:
function f(): void
. - Multi catch exception handling:
try { ... } catch (Exception1 | Exception2 $e) { ... }
- Class constant visibility:
class MyClass { const PUBLIC_CONSTANT_1 = 1; public const PUBLIC_CONSTANT_2 = 2; protected const PROTECTED_CONSTANT = 4; private const PRIVATE_CONSTANT = 8; }
- Symmetric array destructuring:
$data = [ [1, 'Tom'], [2, 'Fred'], ]; ... [$id, $name] = $data[0]; // list($id, $name) = $data[0]; foreach ($data as [$id, $name]) { // foreach ($data as list($id, $name)) ... }
- Support for keys in list():
$data = [ ['id' => 1, 'name' => 'Tom'], ['id' => 2, 'name' => 'Fred'], ]; list('id' => $id, 'name' => $name) = $data[0]; foreach ($data as list('id' => $id, 'name' => $name)) { ... }
- object pseudo-type:
function f(object $o)
(in addition to bool, int, float, string, array, iterable, callable, self, parent and interface/class name (and mixed since PHP 8.0)). - Abstract method overriding:
abstract class A { abstract public function f(string $x); } abstract class B { // Maintaining contravariance for parameters and covariance for return abstract public function f($x): int; }
- New functions: array_key_first(), array_key_last(), is_countable().
- Array destructuring supports reference assignments:
[&$id, &$name] = $data[0]
.
- Null coalescing assignment operator:
$array['key'] ??= 'initial value'
. - Typed properties:
public int $id
. - Arrow functions:
fn($n) => $n * $factor
, where $factor is a parent scope variable. - Unpacking inside arrays:
$fruits = ['banana', 'orange', ...$otherFruits]
. - Numeric literal separator:
299_792_458; // Decimal number
.