PHP new syntax reference

PHP introduces new language features with every release. As such, it can be difficult keeping track of them all and remembering which features can be used in which version. So below we list the major syntax changes in each version along with a short example.

PHP 7.4

Typed properties

Type declarations on class variables.

class Foo
{
    public int $bar;
    private string $qux;
}

Arrow functions

Shorthand syntax for anonymous functions; includes automatic variable binding (no need for ‘use’).

$foo = 3;
$multiplyFoo = fn($n) => $n * $foo;

Null coalesce assignment operator

$foo['bar'] ??= $defaultValue;
// equivalent to:
$foo['bar'] = $foo['bar'] ?? $defaultValue;

Array unpacking

The “splat” operator can be used inside arrays.

$foo = ['C', 'D'];
$bar = ['A', 'B', ...$foo, 'E'];

Number separator

$oneMillion = 1_000_000;

Full list of 7.4 features

PHP 7.3

Trailing commas in function parameters

function foo(
    SomeClass $longArgumentName,
    AnotherClass $otherArgument,
) {
    // etc
}

Full list of 7.3 features

PHP 7.2

Object type keyword

function foo(object $bar)
{
    // etc
}

Parameter type widening

class A
{
    public function foo(Bar $abc)
    {
        // $abc must be of type Bar
    }
}
class B extends A
{
    public function foo($abc)
    {
        // $abc may be any type
    }
}

Trailing comma for namespace groups

namespace Foo\{
    Bar,
    Qux,
}

Full list of 7.2 features

PHP 7.1

Nullable types

Parameter declarations and return types can optionally be null.

function foo(?int $input): ?string
{
    if ($input === null)
        return null;

    return $input . 'foo';
}

Void return type

A void function must not return a value (including null).

function foo(): void
{
    return;
}

Iterable type

function foo(iterable $iter)
{
    foreach ($iter as $elem) {
        //
    }
}

Array destructuring

$array = ['foo', 123];
[$str, $num] = $array;
// equivalent to
list($str, $num) = $array;

Class constant visibility

class Foo
{
    public const BAR = 1;
    private const QUX = 2;
}

Array destructuring with keys

$array = ['id' => 123, 'name' => 'Foo'];
['name' => $name, 'id' => $id] = $array;
// or
list('name' => $name, 'id' => $id) = $array;

Negative string offset

$str = 'foobar';
$a = $str[-2];

Full list of 7.1 features

PHP 7.0

Scalar type hints

function foo(int $num, string $text)
{
    // $num and $text are guaranteed to be an integer and string respectively
}

Return types

function foo(): string
{
    return 'bar';
}

Null coalesce operator

$foo = $array['bar'] ?? 'qux';
// equivalent to
$foo = isset($array['bar']) ? $array['bar'] : 'qux';

Spaceship operator

Operator that returns -1, 0 or 1. For use in sorting/comparison functions.

usort($array, function($a, $b) {
    return $a <=> $b;
};

Anonymous classes

$obj = new class {
    function foo() { return 1; }
};

$num = $obj->foo();

Namespace groups

use Foo\{Bar, Qux};
// equivalent to
use Foo\Bar;
use Foo\Qux;

Full list of 7.0 features

PHP 5.x

A quick summary of features in older PHP versions.

PHP 5.6Splat operator, exponentiation operator, use function
PHP 5.5Generators, class name resolution, finally for try/catch
PHP 5.4Short array syntax, traits, short echo tag always available
PHP 5.3Namespaces, closures, shorthand ternary operator, goto