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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:08:07
Original
913 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的三元符的解释是从左到右的,
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template