Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有一些捷径可以做到这一点
if (!empty($b)) $a = $b; else if (!empty($c)) { $a = $c;
我知道你可以使用三元运算符,但它不是我在 JavaScript 中所要求的,有办法像这样分配
my_var = some_Var || fu_bar || 0;
因此,如果第一个不存在,则使用第二个,如果第二个不存在,则使用第三个。
php中有类似的东西吗?
您可以使用适合您的两种方式之一。这两个函数都会检查我们是否有非空变量。
1: $a = (isset($b) && $b)?$b:$c; 2: if($b) $a = $b; elseif($c) $a = $c;
想不出除此之外的其他方法:
$a = ! empty($b) ? $b : (! empty($c) ? $c : 0)