views:

663

answers:

8

Can someone please explain what the "?" and ":" operators are in PHP?

e.g.:

(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)
+29  A: 

This is the conditional operator.

$x ? $y : $z

means "if $x is true, then use $y; otherwise use $z".

People will tell you that ?: is "the ternary operator". This is wrong. ?: is a ternary operator, which means that it has three operands. People wind up thinking its name is "the ternary operator" because it's often the only ternary operator a given language has.

chaos
If possible it should be avoided other than for the most simple cases. IMHO it makes the old diffiuclt to read.
HeretoLearn
I don't disagree, but I sometimes feel that people take their conditional operator hate too far. I would say that ?: should be used whenever it makes the code cleaner and clearer -- and that we can assume, for purposes of "clearer", that the person reading the code does readily understand what ?: does.
chaos
I love conditional operators... :(
micmoo
Well, they love you too. :)
chaos
I'm fond of the conditional operator for return statements (where a condition is necessary, obviously.) One return keyword, two conditions. :)
Yohnny
Of course, in good languages, "if, then, else" is an expression. ;)
Deniz Dogan
Personally i love condition expressions and find them very easy to read. Once you understand that they're an 'IF' shortcut, you wonder what was so hard about them. But each to his own i guess. :o)
Gary Willoughby
I literally thought it was called the ternary operator. Thanks for the clarification.
Tom
From the php docs http://php.net/manual/en/migration53.new-features.php: The ternary operator now has a shorthand form: ?: Just because a word describes a thing doesn't mean it can't also be the name for it.
grossvogel
@grossvogel: No, but if that word actually or potentially describes *an infinite number of other things*, then yes, that does mean it can't be the name for it. And the fact that you point out makes Zend *even stupider*, since now they're saying that "the ternary operator", as they moronically call it, has a form *WHERE IT IS A BINARY OPERATOR*.
chaos
@chaos: We learn this from our teachers, in schools or on the job. We then replicate it. It is good to correct us, as we are victims of maleducation.
michaelc
+6  A: 

It's called a ternary operator. If the first expression evaluates to true, HTTPS_SERVER is used, else HTTP_SERVER is chosen.

It's basically a shorthand if statement, the above code could also be rewritten as follows:

if ($request_type == 'SSL') {
   HTTPS_SERVER;
} else {
   HTTP_SERVER;
}
John T
It's not *the* ternary operator, just *a* ternary operator. Ternary means that the function (operator) takes three arguments.
Deniz Dogan
http://en.wikipedia.org/wiki/Ternary_operator
John T
Yes, Wikipedia is on my side in this case. :)
Deniz Dogan
See also http://stackoverflow.com/questions/798545/what-is-the-java-operator-called-and-what-does-it-do
chaos
A: 

That is a one line if statement:

condition ? true : false

Translated to an ordinary if statement in your case, that would be:

if($request_type == 'SSL') HTTPS_SERVER;
else HTTP_SERVER;
Yngve Sneen Lindal
Not quite; the operator evaluates to a value, which your code doesn't do.
rascher
Precisely. I don't know PHP, but in many other languages, "if" is a statement, not an expression. ?: returns an expression, not a statement.
Deniz Dogan
Re-read his example: the OP discards the value of the operator, so your code is technically okay!
rascher
Ok, but I think I gave an answer that explained to him what the one line if sentence would do.
Yngve Sneen Lindal
+5  A: 

This is sometimes known as the ternary conditional operator. Ternary means that it has three arguments, as x ? y : z. Basically, it checks if x is true; if it is, then put y instead of this operation, otherwise z.

$hello = $something ? "Yes, it's true" : "No, it's false";
Deniz Dogan
+1  A: 

That's basically a fancy way if writing an if else statement.

Some say its easier to read, some say not.

ternary operator at wikipedia

Daniel T. Magnusson
It's easier to read for short statements. Once you start adding complex expressions or nesting them, then you should switch to if/else.
DisgruntledGoat
I agree, but its very subjective, and pretty hard to read if you don't know how it works, see this question ;p
Daniel T. Magnusson
A: 

As John T says, it is called a ternary operator and is essentially a shorthand version of an if /else statement. Your example, as a full if / else statement, would read;

if($request_type == 'SSL')
{
    HTTPS_SERVER;
}
else
{
    HTTP_SERVER;
}
MatW
Right, but the entire if-else block would have to evaluate to something in order for it to be equivalent to the operator. But since the OP discards the value... I guess this code is okay?
rascher
Not sure what you mean. The only equivalence that matters here is functional equivalence, no?
MatW
+1  A: 

This is a short way of writting IF sentences. It is also used in other languages like Java, Javascript and others.

Your code:

$protocol = $request_type == 'SSL' ? HTTPS_SERVER : HTTP_SERVER;

can be written like this:

if($request_type == 'SSL')
    $protocol = HTTPS_SERVER;
else
    $protocol = HTTP_SERVER;
xpepermint
A: 

Conditional operator ?: is an operator which is used to check a condition and select a value depending on the value of the condition. It is expressed in the following form:

             variable = condition ? expression1 : expression2;

It works as follows...

  1. Firstly, condition is evaluated.
  2. If the condition is true, then expression1 is evalauated. And the value of expression1 is assigned to the variable.
  3. If the condition is false, then expression2 is evaluated. And the value of expression2 is assigned to the variable.

For example:

                    x = (a>b) ? 5 : 9

In this, for x, firstly the condition (a>b) is evaluated. If this condition becomes true, then x will become the value 5 (ie, x=5). But if the condition (a>b) becomes false, then x will attain the value 9 (ie, x=9).

Ternary Operator

Sometimes conditional operator ?: is also called a ternary operator. This is so because it involves three operands. For example:

            x ? y : z

Here, x,y and z are the three operands. If condition x is true, then value y is assigned otherwise value z is assigned.