views:

72

answers:

4

Possible Duplicate:
Reference - What does this symbol mean in PHP?

I've been doing conditionals with if/else or a year or so now. Looking at some new code, I'm seeing a conditional that appears to use ? and : instead of if and else. I'd like to learn more about this but am not sure what to google to find articles explaining how it works. Help?

+5  A: 

It's the Ternary Operator.

Basic usage is something like

$foo = (if this expressions returns true) ? (assign this value to $foo) : (otherwise, assign this value to $foo)

It can be used for more than assignment though, it looks like other examples are cropping up below.

I think the reason you see this in a lot of modern, OO style PHP is that without static typing you end up needing to be paranoid about the types in any particular variable, and a one line ternary is less cluttered than a 7 line if/else conditional.

Also, in deference to the comments and truth in naming, read all about the ternary operators in computer science.

Alan Storm
It’s *a* ternary operator and not *the* ternary operator.
Gumbo
@Gumbo - I thought the same thing. Unfortunately the PHP documentation has it backwards. It says one of the conditional operators is the ternary operator. One more reason I don't like PHP...:-P
Justin Niessner
In the context of PHP it's THE ternary operator, as enshrined in the manual page. Take it up with them.
Alan Storm
@Justin Niessner: I know it’s often referred to as “the” ternary operator as it’s the only ternary operator in PHP I know of. But what else conditional operators are there that it’s only referred to as being one of them?
Gumbo
@Gumbo - I have no idea. Like I said, I think their documentation is backwards too (hence my answer).
Justin Niessner
Wikipedia link added to clear up pesky things like "truth in naming"
Alan Storm
A: 

This is the ternary operator in PHP. It's shorthand for if/else, format is:

condition ? true expression : false expression;
Jason McCreary
It’s *a* ternary operator and not *the* ternary operator.
Gumbo
@Gumbo, seriously?
Jason McCreary
Gumbo
@Gumbo. Thanks for the deep insight. I wasn't sure if you were serious. As a moderator of this community I didn't expect you to begrudge an active participant such things. I've seen a lot of that this week. I guess it's understandable as syntax is our life. However, I hope that doesn't become a trend of this community.
Jason McCreary
@Jason McCreary: I just like to use a proper terminology. But it might be pedantic in this case.
Gumbo
+1  A: 

condition ? do_if_true : do_if_false

So, for example

$true = 1;
$false = 0

$true ? $do->something() : $do->nothing();

in the above, do->something() will be ran.

But in the below example, do->nothing() will be ran.

$false ? $do->something() : $do->nothing();
Rafael
+2  A: 

That would be the conditional operator. It's pretty much a single line if/then/else statement:

if(someCondition){
    $x = doSomething();
}
else{
    $x = doSomethingElse();
}

Becomes:

$x = someCondition ? doSomething() : doSomethingElse();
Justin Niessner