1

Possible Duplicate:
What is the PHP ? : operator called and what does it do?

What does this mean in longform? I haven't seen a line like this.

$max_o = $max_o > $o ? $max_o : $o;
Community
  • 1
  • 1
Collin White
  • 640
  • 1
  • 11
  • 27

5 Answers5

5

This is the same as saying:

if($max_o <= $o) {
    $max_o = $o;
}

But in longhand it says

if($max_o > $o) {
    $max_o = $max_o;
}
else {
    $max_o = $o;
}

Which is rather pointless. This is a poor usage of the ternary operator since my first simple example would do just as well.

Abel Mohler
  • 785
  • 5
  • 12
1
if ($max_o > $o)
{
    $max_o = $max_o;
}
else
{
    $max_o = $o;
}
Syjin
  • 2,740
  • 1
  • 27
  • 31
1

This is the php ternary operator. It is like the following code:

if ($max_o > o)
  $max_o = $max_o;
else
  $max_o = $o;
Yet Another Geek
  • 4,251
  • 1
  • 28
  • 40
1

The question mark is a ternary operator

it is the same as typing

if ($max_o > $o) {
    $max_o = $max_o;
} else {
    $max_o = $o;
}
dqhendricks
  • 19,030
  • 11
  • 50
  • 83
1

What you actually want to write is:

$max_o = max($max_o, $o);

Apart from that, the thing's called ternary operator and is a shortcut syntax for an if-statement.

NikiC
  • 100,734
  • 37
  • 191
  • 225