Skip to main content

PHP - New features cheatsheet from 7.0 to 7.2

Short summary with commonly used new features for PHP 7.0-7.2

Scalar, strict, return type declarations; Null coalescing operator; spaceship; constant arrays; anonymouse classes; group namespaces; yield...

Selected changes for PHP 7.0 with short examples. More informations can be found on:


Scalar type declarations

bool, float, int, string

function scriptun(bool $a, float $b, int $c, string $d){}

Strict types declaration

Wrong type of variable will cause exception


Return type declarations

function mustReturnArray(): array{
    return []; // or exception if not an array

Null coalescing operator

Shortcut for conditional isset

$variable = $_GET['scriptun'] ?? 'empty';
// This is equivalent to:
$variable = isset($_GET['scriptun']) ? $_GET['scriptun'] : 'empty';

Spaceship operator: $a <=> $b

It returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b

echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

Constant can return array

define('SCRIPTUN', [ 'a',  'b',  'c']);
echo SCRIPTUN[1]; // outputs "b"

Anonymous classes

$anonymousClass = new class extends SomeExistingClass {
    public function myfunction() {}

Filtered unserialize()

allowed_classes default is false

// converts all objects into __PHP_Incomplete_Class object
$data = unserialize($foo, ["allowed_classes" => false]);
// converts all objects into __PHP_Incomplete_Class object
// except those of MyClass and MyClass2
$data = unserialize($foo, 
  ["allowed_classes" => ["MyClass", "MyClass2"]]

Group use declarations

use some\namespace\{ClassA, ClassB, ClassC as C};

Generator Return Expressions

Documentation on

$gen = (function() { yield 1; yield 2; return 3; })();
foreach ($gen as $val) { echo $val, PHP_EOL; } // 1,2,3
// or

Integer division with intdiv()

var_dump(intdiv(10, 3));
nullable return types, void, list = [], class constants visibility, multi catch exception, keys in list function...

Selected changes for PHP 7.1 with short examples. More informations can be found on:

Nullable types

function mustReturnArray(): array{
    // must be array on PHP 7.0, 
    // return null throws exception
    return [];
function mayReturnArray(): ?array{
    return null; // Can be array or null

Void functions

function returnNothing(): void{
    echo 'hello';
   // we can end here or break this function:
   return; // this is correct
   return null;  // this will throw exception

Symmetric array destructuring

function list can be replaced with []

// list() style
list($id1, $name1) = $data[0];
// [] style
[$id1, $name1] = $data[0];

Class constant visibility

class ConstDemo
    const PUBLIC_CONST_A = 1;
    public const PUBLIC_CONST_B = 2;
    protected const PROTECTED_CONST = 3;
    private const PRIVATE_CONST = 4;

Multi catch exception handling

try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions

Support for keys in list()

$data = [
    ["id" => 1, "name" => 'Tom'],
    ["id" => 2, "name" => 'Fred'],
// list() style
list("id" => $id1, "name" => $name1) = $data[0];
// [] style from PHP 7.1
["id" => $id1, "name" => $name1] = $data[0];

Support for negative string offsets

var_dump("abcdef"[-2]); // string (1) "e"
var_dump(strpos("aabbcc", "b", -3)); // int(3)
object return type, abstract method overriding, parameter type widening, trailing comma for grouped namespaces

Selected changes for PHP 7.2 with short examples. More informations can be found on:

New multibyte string functions

  • mb_chr()
  • mb_ord()
  • mb_scrub()

New object type

function test(object $obj) : object
    return new SplQueue();

Abstract method overriding

abstract class A
    abstract function test(string $s);
abstract class B extends A
    // overridden - still maintaining contravariance
    // for parameters and covariance for return
    abstract function test($s) : int;

Parameter type widening

Parameter types from overridden methods and from interface implementations may now be omitted.

interface A
    public function Test(array $input);
class B implements A
    public function Test($input){} // type omitted for $input

Allow a trailing comma for grouped namespaces

use Foo\Bar\{

Add new comment


This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.



Great summary! It helped me last time when I had an interview! Thanks!

We use cookies on our website to enhance your user experience. We also use Google analytics and ads.

Click here to read more I've read it