What’s new in PHP 7.3

Heredoc и Nowdoc ― Улучшен синтаксис

wiki: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes

The closing marker now may have indentation of TABs or spaces.

The syntax of heredoc and nowdoc had very strict requirements. This led developers to avoid them because their usage in code looked ugly and reduced readability.

Therefore two changes were made:

Разрешить отступ для закрывающего маркера.
// новый синтаксис:
class foo {
	public $bar = <<<EOT
	bar
	EOT;
}

// раньше приходилось писать так:
<?php
class foo {
	public $bar = <<<EOT
bar
EOT;
}

Еще примеры как это работает с отступами:

// no indentation
echo <<<END
	 b
	c
END;
/*
	 b
	c
*/

// 4 spaces of indentation
echo <<<END
	 b
	c
	END;
/*
 b
c
*/
Убрать требование новой строки после закрывающего маркера.

Стало возможно писать так:

stringManipulator<<<END
  b
 c
END);

$values = [<<<END
b
c
END, 'd e f'];

Было так:

stringManipulator(<<<END
   a
  b
 c
END
);

$values = [<<<END
a
b
c
END
, 'd e f'];

list( &$a ) — cсылки в list()

In PHP there has long existed list() and references. However until PHP 7.3 there was no way to use reference assignment with list().

$array = [ 1, 2 ];
list( $a, &$b ) = $array;

// or via destructuring
[ $a, &$b ] = $array;

Of course, it works the same as the usual list(), so you can use it with nested list() and skip values:

$array = [ 1, 2, 3, [3, 4] ];
list( &$a, $b,, list( &$c, $d ) ) = $array;

It also works with the foreach() function:

$array = [ [1, 2], [3, 4] ];
foreach ( $array as list( &$a, $b ) ) {
	$a = 7;
}
var_dump( $array )
/*
array(2) {
  [0]=> array(2) {
	[0]=> int(7)
	[1]=> int(2)
  }
  [1]=> array(2) {
	[0]=> &int(7)
	[1]=>  int(4)
  }
}
*/

is_countable() — функция

This RFC proposes a new function that returns true if the given value is an array type or an instance of the Countable interface.

It used to be checked like:

if ( is_array($foo) || $foo instanceof Countable ) {
	// $foo is countable
}

Now you can do:

if ( is_countable($foo) ) {
	// $foo is countable
}

Other examples:

var_dump(is_countable([1, 2, 3])); // bool(true)
var_dump(is_countable(new ArrayIterator(['foo', 'bar', 'baz']))); // bool(true)
var_dump(is_countable(new ArrayIterator())); // bool(true)
var_dump(is_countable(new stdClass())); // bool(false)
$foo = [ '', [] ];

if ( is_countable( $foo ) ) {
	var_dump( count( $foo ) ); // int(2)
}

array_(key|value)_(first|last)() — новые функции

Поскольку массивы являются мощной структурой данных, в некоторых случаях удобно получить первый или последний ключ/значение массива без обходного пути. Для выполнения этой задачи данный RFC добавляет в ядро четыре функции:

$key = array_key_first( $array );
$key = array_key_last( $array );
$value = array_value_first( $array );
$value = array_value_last( $array );