为什么括号需要
$detect = new Mobile_Detect();
$mobile = $detect->isMobile() and !$detect->isTablet();
VS为什么括号需要
$detect = new Mobile_Detect();
$mobile = ($detect->isMobile() and !$detect->isTablet());
如果我是一个平板电脑(这也被认为是手机),1号例如我的移动越来越真实。 (不期望)。对于第二个例子,我得到了错误(如预期的那样)。
为什么我需要括号,因为它只是一个操作符。
拥有一个more adequate precedence compared to =,使用&&
:
$mobile = $detect->isMobile() && !$detect->isTablet();
从documentation on logical operators:
其原因的两个不同的变体 “和” 和 “或” 运营商是它们操作以不同的优先顺序。
我只是小心地意识到PHP甚至有'和'关键字! –
它的优先级低于赋值。正如你认为PHP不能被设计得比它更差...... –
更适合什么?双方只是一个函数调用。这是没有答案的 –
您需要它们,因为=
符号的优先级高于and
。使用&&
,你可能会失去它们。
哇,这很奇怪。至少我现在知道了。 –
不知道我自己:)。刚刚学会了。 –
'和'的优先级低于'=',我建议避免它,因为它不像'&&'那样直观' – Esailija
'='具有比'和'更高的优先级。你确定你不是指'&&'而是? –