php 变量中的多个值(或)是可能的吗?

multiple value (or) in php variable is possible?

可能是 php 变量中带有条件(或)的多个值。比如这样

$shippingCost = $selectedShipping['cost'] || null;

我试过了,但出现 return 错误

当有条件检查时,我们使用三元运算符为参数赋值。否则你也可以简单地使用 if else 条件

using ternary operator 

$shippingCost = isset($selectedShipping['cost']) ? $selectedShipping['cost'] : null

using if,else

$shippingCost = 0;
if(isset($selectedShipping['cost'])){
  $shippingCost = $selectedShipping['cost'];
}

如果我没记错的话,三元运算符 (?:) 会尝试查找值是真值还是假值,因此如果未设置,它将 return 报错或警告。

您正在寻找的是传递一个值,具体取决于它是否已设置。

过去我们有 isset($array['key'])array_key_exists 供高级人员使用,但是现在您可以使用空合并运算符 - ??.

$array['key'] ?? null - 这将 return 数组中该键下的任何内容,如果未定义则为 null。