0

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 I am not sure what to google to find articles explaining how it works. How can I do it?

Community
  • 1
  • 1
bflora2
  • 723
  • 3
  • 8
  • 24

4 Answers4

6

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
  • 157,413
  • 86
  • 367
  • 554
  • 1
    It’s *a* ternary operator and not *the* ternary operator. – Gumbo Oct 29 '10 at 19:49
  • 1
    @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 Oct 29 '10 at 19:51
  • 2
    In the context of PHP it's THE ternary operator, as enshrined in the manual page. Take it up with them. – Alan Storm Oct 29 '10 at 19:52
  • @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 Oct 29 '10 at 19:56
  • @Gumbo - I have no idea. Like I said, I think their documentation is backwards too (hence my answer). – Justin Niessner Oct 29 '10 at 19:56
  • Wikipedia link added to clear up pesky things like "truth in naming" – Alan Storm Oct 29 '10 at 20:07
3

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
  • 229,755
  • 35
  • 391
  • 521
1

It is:

condition ? do_if_true : do_if_false

So, for example in the below, do->something() will be run.

$true = 1;
$false = 0

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

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

$false ? $do->something() : $do->nothing();
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Rafael
  • 383
  • 2
  • 6
0

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

condition ? true expression : false expression;
Jason McCreary
  • 66,624
  • 20
  • 123
  • 167
  • It’s *a* ternary operator and not *the* ternary operator. – Gumbo Oct 29 '10 at 19:50
  • Yes. *Ternary operator* just means that there are three operands like a binary has two operands (e.g. `a+b`, `a&b`, `a/b`, etc.) and a unary has just one operand (e.g. `!a`, `~a`, `@a`, etc.). But since PHP has just one ternary operator, it’s often referred to as the ternary operation. – Gumbo Oct 29 '10 at 20:19
  • @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 Oct 30 '10 at 13:38
  • I just like to use a proper terminology. But it might be pedantic in this case. – Gumbo Oct 30 '10 at 13:44