1

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

I've been programming PHP for years, but have never understood what this syntax does or means. I'm hoping you guys can explain it to me, it's about time I knew the answer:

list($name, $operator) = (strpos($key, '__')) ? explode('__', $key) : array($key, null);

Specifically, I'm curious about the SOMETHING ? SOMETHING : SOMETHING;

Community
  • 1
  • 1
swt83
  • 2,001
  • 4
  • 25
  • 33

4 Answers4

2

It's shorthand for if() { } else {}.

if($i == 0) {
  echo 'hello';
} else {
  echo 'byebye';
}

is the same as:

echo $i == 0 ? 'hello' : 'byebye';
ceejayoz
  • 176,543
  • 40
  • 303
  • 368
0

The first statement after '?' is executed if the first expression before '?' is true, if not the last is executed. It also evaluates to the value of the executed expression.

rtn
  • 127,556
  • 20
  • 111
  • 121
0

Its conditional operator just like if in simple words if in one line

(condition) ? statement1 : statement2

If condition is true then execute statement1 else statement2

jimy
  • 4,848
  • 3
  • 35
  • 52
0

this is the pure if else tertiary operation

if(a==b) {
    c = 3;
} else {
    c = 4;
}

this is same as

c = (a==b) ? 3:4;
Ujjwal Manandhar
  • 2,194
  • 16
  • 20