$i = 2;
$result = ($i == 2) ? "Two" : ($i == 1) ? "One" : "Other";
echo $result; // outputs: One
雖然相同的代碼在C#輸出:兩個爲什麼PHP會提供意想不到的輸出?
int i=2;
String result = (i == 2) ? "Two" : (i == 1) ? "One" : "Other" ;
Console.Write(result); // outputs: Two
這並不意外,它只是非直觀的....這正是爲什麼文檔建議反對嵌套三元組:'注意: 建議您避免「疊加」三元表達式。在單個語句中使用多個三元運算符時,PHP的行爲是不明顯的# –
您對編譯器沒有心臟。堆疊三元組... –