php中foo,PHP中or运算符的行为

按照关于逻辑运算符的php.net网页:

这个:

$e = false || true;

这样的行为:

$e = (false || true) // If false is true, then $e = false. Otherwise true

这个:

$f = false or true;

会像这样:

($f = false) or true; // $f = false is true, as the assignment succeeded

这个:

$foo or $foo = 5;

会像这样:

$foo or ($foo = 5) // foo = undefined or foo = 5, so foo = 5

对于最后一个,undefined基本上类似于false,因此foo等于5。

另外,这是运算符优先顺序的链接:[http://www.php.net/manual/zh/language.operators.precedence.php]

更新:

好的,现在让我们进入重点。 就像我们都知道使用提取的查询时一样:

while($row = @mysql_fetch_assoc($result))

众所周知,虽然循环仅在true上执行,所以$row = @mysql_fetch_assoc($result)返回true。

与达里奇的问题相同。

$foo or $foo = 5;

基本上是:

$foo or ($foo = 5);

基本上是:

$foo = undefined or ($foo = 5); // $foo = 5 actually returns true

这也是

$foo = undefined or true;

正如我前面提到的,undefined = false,因此$ foo = 5(因为这是正确的语句)。

希望大家都能理解。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值