2011-04-04 74 views
0

可能重复:
PHP - and/or keywords
Ruby: difference between || and 'or''and,or'和'&&,||'有什么区别?

是什么这之间的区别:

a=20 
b=30 
if (a > 10 && b > 10) 
    puts a 
end 

这:

if (a > 10 and b > 10) 
puts a 
end 

也解释了“||”和“或”

+3

你为什么这个标签作为PHP?我删除了PHP标记,并将Rails标记更改为Ruby,因为代码示例是Ruby(或无效的PHP:P)。这里有很多答案都是关于PHP的。我对此感到抱歉。 (虽然这两种语言的答案都是一样的。) – 2011-04-04 11:14:24

+0

由于您标记了[[PHP]],并且所有当前答案都链接到PHP文档,因此我将其标记为[PHP]的完全重复并​​且/或关键字](http://stackoverflow.com/questions/4502092/php-and-or-keywords),你会发现完全相同的答案。 – 2011-04-04 11:15:57

+0

我找到了我的问题的答案。我已经发布了下面的答案。请检查一下。谢谢大家。 – 2011-04-04 12:06:32

回答

6

答案涉及已被分配到这个问题PHP标签,但是,事后又

删除它们与一个小的差别一样。与&&||相比,andor运营商具有lower precedence

manual拿这个例子:

// The result of the expression (true && false) is assigned to $g 
// Acts like: ($g = (true && false)) 
$g = true && false; 

// The constant true is assigned to $h and then false is ignored 
// Acts like: (($h = true) and false) 
$h = true and false; 
1

只有差异precedence

+0

我知道php中的运算符优先级。这对我来说不是一个合适的答案。 – 2011-04-04 11:48:24

1

你可以从优先级表中看到Ruby,差异也优先级,因为它是在PHP中: andor的优先级低于&&||

但与PHP稍有不同:在Ruby中,andor具有相同的优先级。