php中三元運算子的嵌套執行個體

來源:互聯網
上載者:User
// 乍看起來下面的輸出是 'true'echo (true?'true':false?'t':'f');// 然而,上面語句的實際輸出是't',因為三元運算子是從左往右計算的// 下面是與上面等價的語句,但更清晰echo ((true ? 'true' : 'false') ? 't' : 'f');// here, you can see that the first expression is evaluated to 'true', which// in turn evaluates to (bool)true, thus returning the true branch of the// second ternary expression.
/** *先判斷$_GET['a']若成立則判斷(isset($_GET['b']) ? $_GET['b'] : 'other'),因為有括弧,所以 *會當成一個整體運算 */$rs = isset($_GET['a']) ? $_GET['a'] : (isset($_GET['b']) ? $_GET['b'] : 'other');var_dump($rs);/** * 注意:自左向右結合運算 *先判斷$_GET['a']若成立則 則變成 $_GET['a'] ? $_GET['b'] : 'other'; *不可忽視括弧的作用 */$rs = isset($_GET['a']) ? $_GET['a'] : isset($_GET['b']) ? $_GET['b'] : 'other';var_dump($rs);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.