What are the PHP operators "?" and ":" called and what do they do?
Asked Answered
H

10

52

What are the ? and : operators in PHP?

For example:

(($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER)
Hypnosis answered 3/7, 2009 at 17:27 Comment(0)
C
85

This is the conditional operator.

$x ? $y : $z

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

It also has a short form.

$x ?: $z

means "if $x is true, then use $x; 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.

Clarethaclaretta answered 3/7, 2009 at 17:29 Comment(13)
If possible it should be avoided other than for the most simple cases. IMHO it makes the old diffiuclt to read.Acetylene
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.Clarethaclaretta
I'm fond of the conditional operator for return statements (where a condition is necessary, obviously.) One return keyword, two conditions. :)Rasbora
Of course, in good languages, "if, then, else" is an expression. ;)Avera
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)Fetial
I literally thought it was called the ternary operator. Thanks for the clarification.Kizzee
From the php docs 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.Australorp
@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.Clarethaclaretta
@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.Mineraloid
@chaos: A nice -1 to you, for calling other people stupid and for your blatant denial against linguistic evolution. Calling the ?: operator "ternary operator" is well established; the term is by far more common than "conditional operator".Nickey
@nikic: If a hundred thousand people jump off a bridge, they're still all stupid. Words have actual meanings, including the words "ternary" and "operator", and idiots repeating what they don't understand in a lemming-like fashion does not linguistic evolution make.Clarethaclaretta
Extra +1 for debunking "ternary operator". It makes my skin crawl.Fidole
I have no problem calling it "the ternary operator". It's the only three-operand operator I've ever encountered in my career. Enlighten us if you can name an instance of a programming language with any other three-operand operator. On the flip side of the coin, I have a problem with calling it "the conditional operator", implying it's the only operator that can be used as a condition. Yet we have operators like AND and OR which check if the left operand is true, making them conditional operators. It goes by both, and often cited together because they are one and the same.Copt
O
33

I'm going to write a little bit on ternaries, what they are, how to use them, when and why to use them and when not to use them.

What is a ternary operator?

A ternary ? : is shorthand for if and else. That's basically it. See "Ternary Operators" half way down this page for more of an official explanation.

As of PHP 5.3:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

As of PHP 7.0

PHP 7 has new Null Coalesce Operator. This is the same as a ternary but is also called an "isset ternary". This also allows a set of chained ternaries that remove the need for isset() checks.

In PHP 5, if you wanted to use a ternary with a potentially non-existent variable then you would have to perform an isset() at the beginning of the ternary statement:

$result = isset($nonExistentVariable) ? $nonExistentVariable : ‘default’;

In PHP 7, you can now do this instead:

$result = $nonExistentVariable ?? ‘default’;

The Null Coalesce Operator does not work with an empty string, however, so bear that in mind. The great thing about this is you can also chain the operators for multiple checks for multiple variables, providing a sort of backup depending on whether or not each variable in the chain exists:

$user = $userImpersonatingAnotherUser ?? $loggedInUser ?? “Guest”;

In PHP, with systems where a user can login, it is not uncommon for an administrator to be able to impersonate another user for testing purposes. With the above example, if the user is not impersonating another user, and also a logged in user does not exist, then the user will be a guest user instead. Read on more if you don't understand this yet to see what ternaries are and how they are used, and then come back to this bit to see how the new PHP

How are ternaries used?

Here's how a normal if statement looks:

if (isset($_POST['hello']))
{
    $var = 'exists';
}
else
{
    $var = 'error';
}

Let's shorten that down into a ternary.

$var = isset($_POST['hello']) ? 'exists' : 'error';
                 ^            ^     ^    ^     |
                 |           then   |   else   |
                 |                  |          |
          if post isset         $var=this   $var=this

Much shorter, but maybe harder to read. Not only are they used for setting variables like $var in the previous example, but you can also do this with echo, and to check if a variable is false or not:

$isWinner = false;

// Outputs 'you lose'
echo ($isWinner) ? 'You win!' : 'You lose';

// Same goes for return
return ($isWinner) ? 'You win!' : 'You lose';

Why do people use them?

I think ternaries are sexy. Some developers like to show off, but sometimes ternaries just look nice in your code, especially when combined with other features like PHP 5.4's latest short echos.

<?php 
    $array = array(0 => 'orange', 1 => 'multicoloured'); 
?>

<div>
    <?php foreach ($array as $key => $value) { ?>
        <span><?=($value==='multicoloured')?'nonsense':'pointless'?></span>
    <?php } ?>
</div>

<!-- Outputs:
    <span>
        pointless
    </span>
    <span>
        nonsense
    </span> 
-->

Going off-topic slightly, when you're in a 'view/template' (if you're seperating your concerns through the MVC paradigm), you want as little server-side logic in there as possible. So, using ternaries and other short-hand code is sometimes the best way forward. By "other short-hand code", I mean:

if ($isWinner) :
    // Show something cool
endif;

Note, I personally do not like this kind of shorthand if / endif nonsense

How fast is the ternary operator?

People LIKE micro-optimisations. They just do. So for some, it's important to know how much faster things like ternaries are when compared with normal if / else statements.

Reading this post, the differences are about 0.5ms. That's a lot!

Oh wait, no it's not. It's only a lot if you're doing thousands upon thousands of them in a row, repeatedly. Which you won't be. So don't worry about speed optimisation at all, it's absolutely pointless here.

When not to use ternaries

Your code should be:

  • Easy to read
  • Easy to understand
  • Easy to modify

Obviously this is subject to the persons intelligence and coding knowledge / general level of understanding on such concepts when coming to look at your code. A single simple ternary like the previous examples are okay, something like the following, however, is not what you should be doing:

echo ($colour === 'red') ? "Omg we're going to die" :
     ($colour === 'blue' ? "Ah sunshine and daisies" :
     ($colour === 'green' ? "Trees are green"
     : "The bloody colour is orange, isn't it? That was pointless."));

That was pointless for three reasons:

  • Ridiculously long ternary embedding
  • Could've just used a switch statement
  • It was orange in the first place

Conclusion

Ternaries really are simple and nothing to get too worked up about. Don't consider any speed improvements, it really won't make a difference. Use them when they are simple and look nice, and always make sure your code will be readable by others in the future. If that means no ternaries, then don't use ternaries.

Omnivore answered 16/7, 2013 at 10:47 Comment(4)
if/else statements can in some situations be a lot faster. From the referenced blog post you can read: "On my laptop, snippet 1 [ternary] takes more than two seconds, whereas snippet 2 [if/else] takes about 0.05ms. That's a big difference! But if the variable to test does not host many data, the speed is almost the same."Downstream
@Downstream Did you miss: "It's only a lot if you're doing thousands upon thousands of them in a row, repeatedly."?Omnivore
The difference between ternary and if/else (with huge arrays) is 2 seconds and not 0.5 ms. This was benchmarked using only 100 iterations not thousands. This could be one more argument to use if/else instead of ternary...Downstream
Mine wasn't exactly a scientific explanation, it was rather generic. If you'd like to add a few more numbers and change the non-specific parts, feel free to edit it and I'll accept the edit.Omnivore
T
13

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, and the above code could also be rewritten as follows:

if ($request_type == 'SSL') {
   HTTPS_SERVER;
}
else {
   HTTP_SERVER;
}
Teel answered 3/7, 2009 at 17:29 Comment(8)
It's not the ternary operator, just a ternary operator. Ternary means that the function (operator) takes three arguments.Avera
Yes, Wikipedia is on my side in this case. :)Avera
See also #799045Clarethaclaretta
This is the most explanatory answer.Pyrophyllite
@Clarethaclaretta You are right, in Java, it's called the conditional operator. PHP does not.Doloritas
@Pyrophyllite This code is plagiarized from Mathew's answer at 7/3/09 17:32 UTC.Doloritas
@Userthatisnotauser but didn't John T replied first? At 17:29.Pyrophyllite
Nice find, @Userthatisnotauser! :) And, just as there, this (same) code in this form doesn't make any sense either, anyway. BTW, Mathew's answer got downvoted to ground, possibly for that reason. (That "functional equivalence" argument at Matthew's has no merit in this PHP context of mixing up an expression with a well-defined value and statements that do nothing...)Machree
A
7

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";
Avera answered 3/7, 2009 at 17:30 Comment(0)
F
6

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.

Fulviah answered 23/8, 2009 at 22:52 Comment(0)
U
3

This is a short way of writing 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;
Undershorts answered 3/7, 2009 at 17:50 Comment(1)
Thanks for assigning $protocol correctly to HTTPS_SERVER and HTTP_SERVER unlike the other answers. +1Doloritas
A
0

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;
Aggravate answered 3/7, 2009 at 17:30 Comment(4)
Not quite; the operator evaluates to a value, which your code doesn't do.Dionedionis
Precisely. I don't know PHP, but in many other languages, "if" is a statement, not an expression. ?: returns an expression, not a statement.Avera
Re-read his example: the OP discards the value of the operator, so your code is technically okay!Dionedionis
Ok, but I think I gave an answer that explained to him what the one line if sentence would do.Aggravate
L
0

That's basically a fancy way of writing an if-else statement. Some say it's easier to read, some say not.

Ternary operator at Wikipedia

Lamp answered 3/7, 2009 at 17:31 Comment(3)
It's easier to read for short statements. Once you start adding complex expressions or nesting them, then you should switch to if/else.Particle
I agree, but its very subjective, and pretty hard to read if you don't know how it works, see this question ;pLamp
This is basically a link-only answer.Omnivore
S
0

This works like an if statement it's very simple and easy once you get used to it. (conditions_expressions) ? what_to_do_if_true : what_to_do_if_false.

Sukey answered 27/4, 2020 at 22:44 Comment(1)
Your answer is not really adding anything new. Almost all other answers covered this back in 2009!Mikaelamikal
G
-1

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;
}
Garthgartner answered 3/7, 2009 at 17:32 Comment(4)
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?Dionedionis
Not sure what you mean. The only equivalence that matters here is functional equivalence, no?Garthgartner
That doesn't make sense. poundifdef is saying that this if statement is not completely ("functionally" or any other kind) equivalent to the ternary operator because the whole if/else statement doesn't evaluate to something. See xpepermint's answer below.Doloritas
Sure, the conditional operator evaluates to something, it would be useless if not. But the OP's example contains no assignment at all, so my example is exactly equivalent (if not the most useful way to answer the question, granted)Garthgartner

© 2022 - 2024 — McMap. All rights reserved.