2011-04-04 25 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具有相同的優先級。