New PHP 5 syntax reference
We briefly review the major new PHP language syntax introduced in each version since 5.3 – the version that birthed the features that paved the way for the prevailing PHP renaissance.
Always | Every project. |
Common | Most projects. |
Uncommon | Some projects. |
Rare | Almost never. |
Features are ordered by how frequently you are likely to use them from a practical programming perspective according to the table.
If a feature does not appear in this reference it is because it is not syntax or is a minor modification of existing syntax.
New in PHP 5.3
Released June 30th, 2009.
Feature | Examples | Frequency |
---|---|---|
Namespaces |
namespace Foo;
namespace { $global; }
|
Always |
Closures |
$callable = function($x, $y) use ($local) {};
|
Common |
Ternary shorthand | $this ?: $that; |
Common |
Late static binding | static::overrideMe();
| Common |
Nowdoc |
<<<'SQL'
SELECT injection, proof FROM $table;
SQL;
|
Uncommon |
Jump labels | goto hell; |
Rare |
New in PHP 5.4
Released March 1st, 2012.
Feature | Examples | Frequency |
---|---|---|
Short array syntax | ['foo'] |
Always |
Class member access on instantiation | (new Class)->member(); |
Common |
Traits | trait Foo extends Bar {}
class Fubar { use Foo; } |
Uncommon |
Function array dereferencing | $foo->toArray()[0]; |
Uncommon |
Binary number format | 0b10100111001 |
Rare |
New in PHP 5.5
Released June 20th, 2013.
Feature | Examples | Frequency |
---|---|---|
Class name resolution | Foo::class |
Common |
Generators | yield $x; |
Common |
Finally | try { $h = fopen('foo'); }
finally { fclose($h); } |
Common |
Nested array unpacking | foreach ([[1], [2]] as list($n)) {} |
Rare |
Array and string literal dereferencing | [1, 2][$x];
'1234'[$x]; |
Rare |
New in PHP 5.6
Released August 28th, 2014.
Feature | Examples | Frequency |
---|---|---|
Constant scalar expressions | const TWO = 1 + 1; |
Common |
Argument unpacking (splat) | parse(...$argv); |
Uncommon |
Variadic functions | function(...$params) {} |
Uncommon |
Exponentiation operator | 2 ** 10 |
Uncommon |
Use function and use const | use function Name\Space\f;
use const Name\Space\FOO; |
Rare |