Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
私はこのPHPコードを持っています:
echo true ? 'a' : true ? 'b' : 'c';
これの出力は次のとおりです。
b
しかし、私が期待した出力は次のとおりです。
a
PHP の三項演算子は左結合です。
使用する必要があります
echo true ? 'a' : (true ? 'b' : 'c');
コードは次のように評価されるためです。
echo (true ? 'a' : true) ? 'b' : 'c';
次と同等です。
echo (true) ? 'b' : 'c';
結果は'b'
'b'