What is ?: in PHP 5.3? [duplicate]
Asked Answered
C

3

88

Possible Duplicate: What are the PHP operators “?” and “:” called and what do they do?

From http://twitto.org/

<?PHP
    require __DIR__.'/c.php';
    if (!is_callable($c = @$_GET['c'] ?: function() { echo 'Woah!'; }))
        throw new Exception('Error');
    $c();
?>

Twitto uses several new features available as of PHP 5.3:

  1. The DIR constant
  2. The ?: operator
  3. Anonymous functions

  1. What does number 2 do with the ?: in PHP 5.3?

  2. Also, what do they mean by anonymous functions? Wasn't that something that has existed for a while?

Copernicus answered 28/1, 2010 at 8:33 Comment(7)
Has been answered at least twice #1080747 and #2100334Handyman
@gordon, I know what that means on those topics, I thought this was something different because the site said it was NEW as of 5.3 and also I never seen them together like "?:"Copernicus
@Handyman those are only the longer forms. @jasondavis, you have the : and ? backwards in the question title.Acker
ah okay. The new thing is that you can omit the middle part. And anonymous functions (lambda and closures) are a new addition to 5.3 as well, although you could create functions with create_function before.Handyman
this is not a duplicate.Spatola
It is not a duplicate for the considered question. This question is meant by PHP 5.3Sherris
Yes, please, guys with big enough hats, undo the hastened, mistaken "duplicate" flag! The linked other question is about the generic ternary, with old, basic answers, while this is about the the shorthand (which is, in fact, more than just syntactic sugar!). Following that wrong lead (trying to find out a subtlety, in vain) has resulted me wasting too many minutes from my life. Multiply that by the number of people harmed the same way, and it may even become a criminal category. ;)Foret
M
146

?: is a form of the conditional operator which was previously available only as:

expr ? val_if_true : val_if_false

In 5.3 it's possible to leave out the middle part, e.g. expr ?: val_if_false which is equivalent to:

expr ? expr : val_if_false

From the manual:

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

Mclane answered 28/1, 2010 at 8:36 Comment(5)
Thank you! I was confused when I saw them together like that "?:" and I searched but couldn't find anything on it.Copernicus
Hmm very cool. That makes ?: equivalent to || in Javascript!Arkose
As an added bonus, you can "chain" them: $foo = $bar ?: $bazz ?: $yadda ?: $qux; // $foo will be assigned the value of the first truthy variable. Not sure if this is a terrible idea (some dislike nesting ternary operators), but there it is.Sprocket
@Sprocket I would say that is a concise usage of it. Normally, nesting ternary operators is terrible, yes (I would forgive up to one nested layer in some cases). The alternative to the above would be a very long and ugly if/elseif/else block that is just a waste really. As long as there is a comment such as the one you put for inexperienced programmers, then I would find this chaining perfectly acceptable (and actually I'm excited to use it, thanks for pointing it out!).Freidafreight
@Sprocket Actually, to further prove it isn't a terrible idea, PHP 7 introduced the null coalescing operator (I forgot about this myself) which does a similar thing and has the same general syntax - the difference being isset() vs truthy value though.Freidafreight
A
36

The ?: operator is the conditional operator (often refered to as the ternary operator):

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

In the case of:

expr1 ?: expr2

The expression evaluates to the value of expr1 if expr1 is true and expr2 otherwise:

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.

Almsman answered 28/1, 2010 at 8:38 Comment(0)
S
5

Look here:

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.

Anonymous functions: No, they didn't exist before 5.3.0 (see the first note below the examples), at least in this way:

function ($arg) { /* func body */ }

The only way was create_function(), which is slower, quite cumbersome and error prone (because of using strings for function definitions).

Shaeshaef answered 28/1, 2010 at 8:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.