AND 语句的 PHP 简写语法
我正在尝试实现逻辑连接 AND,并且想知道是否允许使用这种速记符号:
$hasPermissions &= user_hasAppPermission($user_id, $permission);
或者我必须这样做:
$hasPermissions = $hasPermissions && user_hasAppPermission($user_id, $permission);
I am trying to implement the logical connective AND, and was wondering if this shorthand notation is allowed:
$hasPermissions &= user_hasAppPermission($user_id, $permission);
Or do i have to do this:
$hasPermissions = $hasPermissions && user_hasAppPermission($user_id, $permission);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简写
&=
是一个按位赋值操作 ,这不等于你的第二个陈述。这与执行相同(请注意单个&符号):从我所看到的来看,您的“长”声明看起来很好。
The shorthand
&=
is a bitwise assignment operation, which is not equivalent to your second statement. That would be the same as doing (note the single ampersand):From what I can see, your "long" statement seems fine as is.
在 PHP 中,可以使用以下逻辑操作:
AND
或
非
异或
另外,还有一个看看在此页面。两个运算符
&&
和||
与and
和or
具有不同的优先级。因此,您的第二个选项是正确的选择:
顺便说一句:我建议始终使用
===
来比较是否相等。 === 确保其操作数的类型和值相同,而==
则对值进行强制转换。In PHP, these logical operations are available:
AND
OR
NOT
XOR
Additionally, have a look at this page. The two operators
&&
and||
have a different precedence asand
andor
.Thus, your second option is the way to go:
BTW: I'd propose to always use
===
to compare for equality. === ensures that the types of its operands are identical and the values are, while==
casts values.我会做类似的事情:
I would do something like: