开发者

PHP Programming Syntax Question 'x ? x : x;' [duplicate]

This question already has answers here: Closed 11 years ago.

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;


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

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

is the same as:

echo $i == 0 ? 'hello' : 'byebye';


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.


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


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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜