[英]PHP Shorthand Syntax to AND statements
I am trying to implement the logical connective AND, and was wondering if this shorthand notation is allowed: 我正在尝试实现逻辑连接AND,并且想知道是否允许使用这种速记符号:
$hasPermissions &= user_hasAppPermission($user_id, $permission);
Or do i have to do this: 还是我必须这样做:
$hasPermissions = $hasPermissions && user_hasAppPermission($user_id, $permission);
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): 这将与执行操作相同(请注意单个“&”号):
$hasPermissions = $hasPermissions & user_hasAppPermission($user_id, $permission);
From what I can see, your "long" statement seems fine as is. 从我所看到的,您的“长”语句按原样看起来还不错。
In PHP, these logical operations are available: 在PHP中,这些逻辑操作可用:
AND 和
$val1 && $val2
$val1 and $val2
OR 要么
$val1 || $val2
$val1 or $val2
NOT 不
! $val
XOR 异或
$val1 xor $val2
Additionally, have a look at this page . 另外, 请看此页面 。 The two operators &&
and ||
两个运算符&&
和||
have a different precedence as and
and or
. and
和or
优先级不同。
Thus, your second option is the way to go: 因此,您的第二个选择是方法:
$hasPermissions = $hasPermissions && user_hasAppPermission($user_id, $permission);
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. ===确保其操作数的类型相同且值相同,而==
强制转换值。
我会做类似的事情:
$hasPermissions = (($hasPermissions) && (true === user_hasAppPermission($user_id, $permission))) ? true : false;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.