1

Possible Duplicates:
quick php syntax question
Reference - What does this symbol mean in PHP?

$row_color = ($row_count % 2) ? $color1 : $color2;

Community
  • 1
  • 1
parmo
  • 19
  • 1
  • 2
    possible duplicate of one million existing questions, for example [quick php syntax question](http://stackoverflow.com/questions/889373/quick-php-syntax-question) – user229044 Jan 13 '11 at 15:39
  • Let's see: [What is ?: in PHP 5.3?](http://stackoverflow.com/questions/2153180/what-is-in-php-5-3), [What is the PHP ?: operator called and what does it do?](http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do), [?: operator in PHP](http://stackoverflow.com/questions/1993409/operator-php) and [Where can I read about the conditionals done with ? and :](http://stackoverflow.com/questions/4055355/where-can-i-read-about-conditionals-done-with-and). Also see: http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – ircmaxell Jan 13 '11 at 15:40

7 Answers7

5

This is called Ternary operator. Basically it is checking if row_count is odd number then assign row_color to color1 or else color2

Teja Kantamneni
  • 17,402
  • 12
  • 56
  • 86
3

it is extended IF syntax

it equals to

if ($row_count % 2)
  $row_color = $color1;
else
  $row_color = $color2;
heximal
  • 10,327
  • 5
  • 46
  • 69
2

It's the ternary operator

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
1

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);
John Parker
  • 54,048
  • 11
  • 129
  • 129
1

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

Greg
  • 21,235
  • 17
  • 84
  • 107
0

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.

DOK
  • 32,337
  • 7
  • 60
  • 92
0

It's called a ternary operator. A description can be found here: http://php.net/manual/en/language.operators.php

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104