CodeLair website tutorials, articles & scripts

PHP new syntax reference

Published: 31 January 2020   •   Updated: 8 October 2020   •   Tags: php

PHP is under constant development and introduces several 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. If you are using a slightly older version of PHP you’ll need to make sure you don’t start using syntax that’s not yet available in your version.

The same goes if your server uses a different version of PHP to your development environment (although there are better ways to solve that problem such as Docker or Vagrant), or if you’re an open source developer supporting a certain version of PHP. So below we list the major syntax changes in each version along with a short example.

Update: I’ve added changes from the upcoming PHP 8 to this article. Although very few people will be using it yet, it’s useful to know what’s there and ensure you don’t get confused between trailing commas in function calls (PHP 7.3) and function definitions (PHP 8.0).


PHP 8.0 #

Union types

Function/method parameters can declare multiple types using the pipe character |.

class Foo
{
public int|float $number;

public function bar(Collection|array $list)
{
// etc
}
}

Mixed type hint

Function/method arguments or return values can explicitly be declared as mixed type, which is equivalent to array|bool|callable|int|float|null|object|resource|string.

function foo(mixed $anything): mixed
{
if ($anything === 0)
return 0;

return 'not zero';
}

Trailing commas in function definitions

A comma can be used after the final argument in a function/method definition. Useful when there are many arguments split over multiple lines. Trailing commas in function calls were permitted as on PHP 7.3.

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

Static return type

Late static binding can be used as a return type.

class A
{
public function foo(): static
{
return new static;
}
}
class B extends A {}

(new A)->foo(); // returns A
(new B)->foo(); // returns B

Class resolution of variables

Previously only ClassName::class was possible, now it can be used on instance variables.

$object::class;

Catch exceptions without variable

The Exception variable is no longer required.

try {
// something that throws an exception
} catch (Exception) { // previously (Exception $ex)
exit;
}

Attributes

Attributes (aka annotations) can be added to properties and functions, and read from external code using Reflection.

#[MyAttribute("param")]
class Foo {}

class Bar
{
#[PlainAttribute]
public function qux() {}
}

// using attributes
$reflect = new ReflectionClass('Foo');
$attributes = $reflect->getAttributes();

Constructor property promotion

Syntactic sugar to set class properties in the constructor.

class Foo {
public function __construct(
public string $name,
private int $id
) {
// etc
}
}

// equivalent to:
class Foo {
public string $name;
private int $id;

public function __construct(
string $name,
int $id
) {
$this->name = $name;
$this->id = $id;
// etc
}
}

Nullsafe operator

Use ?-> to call methods and properties without needing to constantly check variable types.

$foo = $bar?->baz()?->qux;

// same as
$foo = null;
if ($bar instanceof ClassWithBaz) {
$a = $bar->baz();
$foo = $a->qux ?? null;
}

Named Arguments

Function/method parameters can be passed in any order using their names.

htmlspecialchars($string, double_encode: false);
// equivalent to:
htmlspecialchars($string, ENT_COMPAT|ENT_HTML401, 'UTF-8', false);

PHP 8 upgrade notes


PHP 7.4 #

Typed properties

Type declarations on class member 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;
// equivalent to:
$multiplyFoo = function($n) use ($foo) {
return $n * $foo;
}

Null coalesce assignment operator

Shortcut for setting a variable which may not exist yet.

$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

Underscores can be added to numbers, and are ignored.

$oneMillion = 1_000_000;
// same as
$oneMillion = 1000000;

Full list of 7.4 features


PHP 7.3 #

Trailing commas in function calls

When calling a function or method, a trailing comma can be used, similar to arrays. Useful for long argument lists split onto multiple lines.

Note that trailing commas are still not allowed in function definitions prior to PHP 8.

myVeryLongFunctionName(
$myLongArgumentName,
$heresAnotherVariable, // comma now allowed
);

Full list of 7.3 features


PHP 7.2 #

Object type keyword

Allows generic object as a parameter/return type hint.

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

Parameter type widening

Overridden methods in extended classes or implemented interfaces may widen the parameter type hint (or remove it).

class General {}
class Specific extends General {}

class A
{
public function foo(Specific $abc)
{
// $abc must be of type Specific
}
}
class B extends A
{
public function foo(General $abc)
{
// $abc may be any General class, including Specific
// could remove type hint entirely
}
}

Trailing comma for namespace groups

A final comma can be used in grouped namespaces, similar to arrays.

namespace Foo\{
Bar,
Qux, // comma now allowed
}

Full list of 7.2 features


PHP 7.1 #

Nullable types

Parameter declarations and return types can optionally be null by adding a question mark to the type hint.

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

A type hint specifying an array or an object that implements the Traversable interface.

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

Class constant visibility

A const in classes can be public, private or protected.

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

Array destructuring

Use the short array syntax to save array values to separate variables.

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

Array destructuring with keys

The list() construct can now specify which keys of an associative array to extract. Also works with short array syntax as above.

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

Negative string offset

Use square brackets to reference a specific character in a string, seeking backwards from the end.

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

Full list of 7.1 features


PHP 7.0 #

Uniform variable syntax

Changes to variable semantics including nested operators and operations on any expression in parentheses.

$foo()['bar']();
$foo::$bar::$baz;

// (...) is something that returns an array with key 'foo'
(...)['foo'];
// (...) is something that returns an object
(...)->bar();

// immediately-invoked function expression
(function() { ... })();

Scalar type hints

Use primitive types for function type hints: string, int, float, bool.

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

Return types

Declare that a function will return a specific type or class.

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

Null coalesce operator

Check a potentially null variable without generating warnings.

$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

Create temporary objects to save writing a whole class. Can be useful to make quick objects implementing a simple interface, to pass into other functions.

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

$num = $obj->foo();

Namespace groups

Use multiple namespaces that have similar paths, in one line.

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

Array constants

Arrays can be defined as constants.

define('PRIMES', [2, 3, 5, 7]);

Full list of 7.0 features


PHP 5.x #

Here’s a quick summary of features in older PHP versions.

Version Features
PHP 5.6 Splat operator (...), exponentiation operator ($a**2), use function
PHP 5.5 Generators, ::class, finally for try/catch
PHP 5.4 Short array syntax, traits, short echo tag always available
PHP 5.3 Namespaces, closures, shorthand ternary operator, goto