可能显示的文件:
quick php syntax question
Reference - What does this symbol mean in PHP?是什么?签署此声明?
$ row_color =($ ROW_COUNT%2)? $ color1:$ color2;
可能显示的文件:
quick php syntax question
Reference - What does this symbol mean in PHP?是什么?签署此声明?
$ row_color =($ ROW_COUNT%2)? $ color1:$ color2;
它扩展IF语法
它等于
if ($row_count % 2)
$row_color = $color1;
else
$row_color = $color2;
这是一个有条件的IF语句。如果rowcount是偶数,则显示一种颜色,如果是奇数,则显示其他颜色。
他们设置交替的行颜色。
问号和冒号是什么使它成为IF。
条件出现在问号(rowcount是偶数)之前。
问号后面的第一项是“then”,即如果条件为真,该怎么办。
冒号后面的项目是“else”,也就是说,如果条件不成立,该怎么办。
许多人喜欢这种语法,因为它的简洁。但是,正如你发现的那样,当你第一次遇到它时,这是一个真正的难题,对谷歌来说很难。
这称为Ternary运营商。基本上它是检查是否row_count
是奇数然后分配给row_color
或color1
其他color2
这是一个三元运算符。按照PHP manual:
表达式(expr1)? (expr2): (expr3)如果expr1 的计算结果为TRUE,则计算为expr2;如果expr1 的计算结果为FALSE,则expr3计算为expr2。
换句话说:
$variable = (IF THIS EVALUTES TO TRUE) ? (ASSIGN THIS) : (IF NOT, ASSIGN THIS);
这就是所谓的一个三元运算符。说明可以在这里找到:http://php.net/manual/en/language.operators.php
这就是所谓的Ternary operation
这是下面的代码的短手表示:
if($row_count % 2) {
$row_color = $color1;
}
else {
$row_color = $color2;
}
这是您的原代码,有评论说:
$row_color = ($row_count % 2) ? // Performs logical expression.
$color1 // If logic is true set original variable to this
: $color2; // Else set original variable to this.
关于三元操作的PHP文档:http://php.net/manual/en/language.operators.php
可能有100万个现有问题的重复,例如[快速php语法问题] – meagar 2011-01-13 15:39:12
让我们来看看:[什么是?]在PHP中5.3?](http://stackoverflow.com/questions/2153180/what-is-in-php-5-3),[什么是PHP?:操作符被调用,它有什么作用?](http:// stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do),[?:PHP中的运算符](http://stackoverflow.com/questions/1993409/operator-php)和[我可以在哪里阅读关于完成的条件?和:](http://stackoverflow.com/questions/4055355/where-can-i-read-about-conditionals-done-with-and)。另请参阅:http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – ircmaxell 2011-01-13 15:40:02