"isset构造"是否有捷径?

问题描述:

我经常写这一行代码:

$myParam = isset($params['myParam']) ? $params['myParam'] : 'defaultValue';

通常,对于嵌套数组,这会使行很长.

Typically, it makes the line very long for nested arrays.

我可以把它缩短吗?

PHP 7将包含??运算符,正是该运算符.

PHP 7 will contain ?? operator that does exactly that.

请参见 https://wiki.php.net/rfc/isset_ternary ,例如:

// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';