php三元演算

WBOY
WBOYOriginal
2016-06-13 11:34:21820browse

php三元运算
$a = 2;
$a == 1 ? $test="企业" : ($a==2 ? $test="地区" : $test="其他地方");

echo $test;


先判断$a是否为1 如果是 直接输出 企业,如果不是 继续判断 相当于else里面又嵌套一个if 如果等于2输出地区 如果不是输出 其他地方

等价于

$a = 2;

if($a == 1) {

echo $test="企业";

} else {
if($a == 2){

$test = "地区";

} else {

$test="其他地方";

}

}

echo $test;


或者

if($a == 1) {

echo $test="企业";

} else {
$a==2 ? $test="地区" : $test="其他地方";

}



Statement:
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