Home » Releases » PHP 7.3 »

PHP 7.3 Code Comparisons

Explore PHP 7.3

Flexible Heredoc and Nowdoc Syntax

PHP 7.3 relaxes the rigid formatting rules for heredoc and nowdoc identifiers. Closing markers can now be indented with spaces or tabs, which also defines the indentation level stripped from each line of the body. Furthermore, the mandatory newline after the closing semicolon is no longer strictly required.
Before PHP 7.3
class Renderer {
    public function render(): string
    {
        return <<<HTML
<div>
    <h1>Hello</h1>
</div>
HTML;
    }
}
PHP 7.3 or Later
class Renderer {
    public function render(): string
    {
        return <<<HTML
            <div>
                <h1>Hello</h1>
            </div>
            HTML;
    }
}

Trailing Commas in Function Calls

PHP 7.3 extends support for trailing commas to function and method parameter lists, as well as calls. This simplifies appending arguments without modifying existing lines, reducing git diff noise.
Before PHP 7.3
$result = array_reduce(
    $items,
    function ($carry, $item) {
        return $carry + $item;
    },
    0
);
PHP 7.3 or Later
$result = array_reduce(
    $items,
    function ($carry, $item) {
        return $carry + $item;
    },
    0,
);

List Destructuring by Reference

Array destructuring in PHP 7.3 supports assignment by reference using the & operator. When destructuring an array into variables, each variable can now be prefixed with & to bind by reference rather than value.
Before PHP 7.3
$point = [1, 2];
$x = $point[0];
$y = $point[1];
$x = 10;
PHP 7.3 or Later
$point = [1, 2];
[$x, $y] = &$point;
$x = 10;
// $point is now [10, 2]

is_countable() Function

Prior to PHP 7.3, checking if a variable was countable before calling count() required complex checks (is_array() || $variable instanceof Countable). The is_countable() function simplifies this verification cleanly.
Before PHP 7.3
$items = get_items();

$count = (
    is_array($items) ||
    $items instanceof Countable
) ? count($items) : 0;
PHP 7.3 or Later
$items = get_items();

$count = is_countable($items)
    ? count($items)
    : 0;