面向对象编码中的管道指的是什么?
我是编码、PHP 和面向对象编程领域的新手。我遇到了以下代码行,想知道 | 的用途以及何时使用它?对我来说它看起来是一个 OR,但我认为 OR 是用 || 表示的
empty($this->contact_country) | empty($this->contact_questcomm) |
I am new to the world of coding, PHP and object oriented programming. I came across the following line of code and was wondering what the | is for as well as when you use it? It looks an OR to me but I thought OR is represented by ||
empty($this->contact_country) | empty($this->contact_questcomm) |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
|
是按位或;||
是逻辑或。这些是PHP语言中的运算符,与OOP无关。对于布尔类型来说,按位和逻辑布尔运算符的工作方式相同,这只是一个巧合,因为 false/true 和 0/1 在 PHP 中可以互换。
例如,
true & false
相当于1 & 0 。结果是
0
,在 PHP 中被认为是假值(即在布尔上下文中是false
)。无论如何,您不应该在条件表达式中使用按位运算符,除非您在条件内进行位移计算。使用逻辑运算符,这些运算符是为条件逻辑而设计的,这样人们就不会感到困惑。和您一样,我肯定会在给定代码中将
|
替换为||
,因为empty()
返回 false 或 true。|
is bitwise OR;||
is logical OR. These are operators in the PHP language and have nothing to do with OOP.It's just a coincidence that bitwise and logical Boolean operators work the same way for Boolean types, as false/true and 0/1 are interchangeable in PHP.
For example,
true & false
is equivalent to1 & 0
. The result is0
, which is considered a falsy value in PHP (i.e. isfalse
in a Boolean context).Anyway, you shouldn't ever be using bitwise operators in conditional expressions, unless you're doing bit-shifting calculations within the conditions. Use logical operators, which are made for conditional logic, so people won't get confused. Like you, I would definitely replace
|
with||
in your given code, asempty()
returns either false or true.