C#中的空合并运算符(?)在PHP中?

在PHP中有没有三元运算符或类似的行为?? 的C#?

?? 在C#中是干净和短,但在PHP中,你必须做的事情如:

 // This is absolutely okay except that $_REQUEST['test'] is kind of redundant. echo isset($_REQUEST['test'])? $_REQUEST['test'] : 'hi'; // This is perfect! Shorter and cleaner, but only in this situation. echo null? : 'replacement if empty'; // This line gives error when $_REQUEST['test'] is NOT set. echo $_REQUEST['test']?: 'hi'; 

PHP 7添加了空的合并操作符 :

 // Fetches the value of $_GET['user'] and returns 'nobody' // if it does not exist. $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; 

你也可以看一下编写php的三元运算符的简短方法吗?:(仅限于php> = 5.3)

 // Example usage for: Short Ternary Operator $action = $_POST['action'] ?: 'default'; // The above is identical to $action = $_POST['action'] ? $_POST['action'] : 'default'; 

而你与C#的比较是不公平的。 “在PHP中,你必须做一些事情” – 在C#中,如果你尝试访问一个不存在的数组/字典项目,你也会遇到一个运行时错误。

空合并操作符 ( ?? )已被接受并在PHP 7中实现。 它与短三元运算符 ( ?: :)的区别在于?? 会压制E_NOTICE ,当试图访问没有密钥的数组时,会发生这种情况。 RFC中的第一个例子给出了:

 $username = $_GET['user'] ?? 'nobody'; // equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; 

注意到?? 操作员不需要手动应用isset来防止E_NOTICE

我使用function。 显然这不是操作员,但比你的方法更清洁:

 function isset_or(&$check, $alternate = NULL) { return (isset($check)) ? $check : $alternate; } 

用法:

 isset_or($_REQUEST['test'],'hi'); 

在PHP 7之前,没有。 如果你需要涉及isset ,使用的模式是isset($var) ? $var : null isset($var) ? $var : null 。 没有包含isset特征的?:操作符。

?? 在C#中是二进制的,不是三进制的。 在PHP 7之前,它没有PHP的等价性。

从PHP 5.6开始,一个相同的运算符不存在,但是可以创build一个类似的函数。

 /** * Returns the first entry that passes an isset() test. * * Each entry can either be a single value: $value, or an array-key pair: * $array, $key. If all entries fail isset(), or no entries are passed, * then first() will return null. * * $array must be an array that passes isset() on its own, or it will be * treated as a standalone $value. $key must be a valid array key, or * both $array and $key will be treated as standalone $value entries. To * be considered a valid key, $key must pass: * * is_null($key) || is_string($key) || is_int($key) || is_float($key) * || is_bool($key) * * If $value is an array, it must be the last entry, the following entry * must be a valid array-key pair, or the following entry's $value must * not be a valid $key. Otherwise, $value and the immediately following * $value will be treated as an array-key pair's $array and $key, * respectfully. See above for $key validity tests. */ function first(/* [(array $array, $key) | $value]... */) { $count = func_num_args(); for ($i = 0; $i < $count - 1; $i++) { $arg = func_get_arg($i); if (!isset($arg)) { continue; } if (is_array($arg)) { $key = func_get_arg($i + 1); if (is_null($key) || is_string($key) || is_int($key) || is_float($key) || is_bool($key)) { if (isset($arg[$key])) { return $arg[$key]; } $i++; continue; } } return $arg; } if ($i < $count) { return func_get_arg($i); } return null; } 

用法:

 $option = first($option_override, $_REQUEST, 'option', $_SESSION, 'option', false); 

这将尝试每个variables,直到find一个满足isset()

  1. $option_override
  2. $_REQUEST['option']
  3. $_SESSION['option']
  4. false

如果4不在那里,它将默认为null

注意:使用引用有一个更简单的实现,但是如果testing的项目不存在,它会将被testing的项目设置为null 。 当数组的大小或真实性很重要时,这可能是有问题的。