Possible Duplicates:
quick php syntax question
Reference - What does this symbol mean in PHP?
$row_color = ($row_count % 2) ? $color1 : $color2;
Possible Duplicates:
quick php syntax question
Reference - What does this symbol mean in PHP?
$row_color = ($row_count % 2) ? $color1 : $color2;
This is called Ternary operator. Basically it is checking if row_count is odd number then assign row_color to color1 or else color2
it is extended IF syntax
it equals to
if ($row_count % 2)
$row_color = $color1;
else
$row_color = $color2;
It's a ternary operator. As per the PHP manual:
The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
In other words:
$variable = (IF THIS EVALUTES TO TRUE) ? (ASSIGN THIS) : (IF NOT, ASSIGN THIS);
This is called a Ternary operation
It is a short hand representation of the following code:
if($row_count % 2) {
$row_color = $color1;
}
else {
$row_color = $color2;
}
Here is your original code, with comments:
$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's documentation on ternary operations: http://php.net/manual/en/language.operators.php
It's a conditional IF statement. If rowcount is even, show one color, and if it's odd, show the other color.
They are setting alternating row colors.
The question mark and the colon are what makes it an IF.
The condition comes before the question mark (rowcount is even).
The first item after the question mark is the "then", that is, what to do if the condition is true.
The item after the colon is the "else", that is, what to do if the condition is not true.
Many people like this syntax because of its brevity. But, as you have found, it is a real puzzle when you first encounter it, and it would be very hard to Google.
It's called a ternary operator. A description can be found here: http://php.net/manual/en/language.operators.php