Home > php教程 > php手册 > php中的三元运算符使用说明

php中的三元运算符使用说明

WBOY
Release: 2016-06-13 12:08:07
Original
863 people have browsed it

今天一个网友在群里发了个题目不难,但是可能会错

复制代码 代码如下:


echo
$a == 1 ? 'one' :
$a == 2 ? 'two' :
$a == 3 ? 'three' :
$a == 4 ? 'foura' : 'other';
echo "\n";


输出结果是:




结果是:four

一开始想不明白,按照我的理解,应该是这样的逻辑:
echo ($a == 1 ? 'one' :
( $a == 2 ? 'two' :
( $a == 3 ? 'three' :
($a == 4 ? 'four' : 'other'))));
输出为:two

后来在kevinG(qq:48474)的指教下,参看php手册,终于明白了php的三元符的解释是从左到右的,
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template