这个赋值结构叫什么?你能用 Php 做吗?
我经常在 Javascript 中使用以下构造:
var foo = other_var || "default_value";
在 Javascript 中,如果左侧为假,则分配右侧的值。
它非常方便,可以节省编写更长且不必要的显式三元表达式。
这种构造有名字吗?
额外奖励:在 Php 中是否有一个技巧可以在不使用三元运算符的情况下执行此操作?
PS:另一种变体是如果没有得到真值则抛出错误,而不是给出默认值:
var foo = something || alert("foo is not set!");
I have often used the following construct in Javascript:
var foo = other_var || "default_value";
In Javascript, if the left side is falsy, then the value on the right side is assigned.
It is very handy, and saves writing longer and unnecessarily explicit ternary expressions.
Is there a name for this sort of construct ?
Bonus: Is there a trick to do this in Php without using a ternary operator?
PS: another variant is to throw an error if you don't get a truthy value, instead of giving a default value:
var foo = something || alert("foo is not set!");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
逻辑或(通常是
||
)运算符在许多语言中都有很大的不同。在某些(如C、C++)中,它的工作方式如下:“计算左侧;如果为真,则返回 true,否则计算右侧,如果为 true,则返回 true否则为 false。” 这里的结果始终是布尔值。
在其他语言中(例如 Javascript、Python,我相信 PHP 也是如此),它更像是:“评估左侧;如果为真,则返回它,否则评估右侧并返回结果。” 然后结果可以是任何类型,并且您可以执行如下构造:
a = (b || c); // 相当于 a = b ? b : c;
或者相当奇特:
The logical-or (usually
||
) operator is drastically different in many languages.In some (like C, C++) it works like: "Evaluate the left-hand side; if it's true, return true, otherwise evaluate the right hand-side and return true if it's true or false otherwise." The result is always boolean here.
In others (like Javascript, Python, I believe that PHP also) it's more like: "Evaluate the left-hand side; if it's true, return it, otherwise evaluate the right-hand side and return the result." Then the result can be of any type and you can do constructs like:
a = (b || c); // equivalent to a = b ? b : c;
or quite fancy:
我相信它只是被称为 OR 结构。这里有很多关于使用作业的好例子:
http://php.net/manual/en/language.operators.assignment.php
I believe it's just called an OR construct. There are a lot of good examples on using assignments here:
http://php.net/manual/en/language.operators.assignment.php
它只是一个逻辑或运算符。请点击链接以获取有关 javascript 的更多信息。
从文档中的示例来看:
编辑:关于奖励,似乎您可以通过执行以下操作对最新版本的 PHP 中的三元进行类似的操作:
来自 PHP 文档:
我对 PHP 不熟悉,所以我有兴趣知道结果。
It is just a logical OR operator. Follow the link for more information in javascript.
From the examples in the docs:
EDIT: Regarding the BONUS, it appears as though you can do something similar with the ternary in recent versions of PHP by doing:
From the PHP docs:
I'm not familiar with PHP, so I'd be interested to know the result.
这是有效的:
上面打印
"1"
因为这是true
的字符串表示形式($mytruevalue
是 true)。然而,这会打印
"20"
因为$myfalsevalue
为 false。如果两个值都等于
false
,则不打印任何内容。希望这有帮助。
This works:
The above prints
"1"
because that is the string representation oftrue
($mytruevalue
is true).This, however, prints
"20"
because$myfalsevalue
is false.If both values are equal to
false
, it prints nothing.Hope this helps.
它是逻辑或运算符。
不,对于 PHP,您只能在 ternay 运算符的帮助下才能做到这一点。
It is the logical OR operator.
No, with PHP, you can do so only with the help of ternay operator.