87

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?

Community
  • 1
  • 1
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 3
    Has been answered at least twice http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do and http://stackoverflow.com/questions/2099834/what-does-this-syntax-in-php-mean – Gordon Jan 28 '10 at 08:43
  • 3
    @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 "?:" – JasonDavis Jan 28 '10 at 08:50
  • 1
    @Gordon those are only the longer forms. @jasondavis, you have the : and ? backwards in the question title. – RJFalconer Jan 28 '10 at 08:54
  • 3
    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. – Gordon Jan 28 '10 at 09:01
  • 6
    this is not a duplicate. – dwenaus May 09 '14 at 04:40
  • 1
    It is not a duplicate for the considered question. This question is meant by PHP 5.3 – SaidbakR Dec 12 '14 at 22:06
  • 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. ;) – Sz. Oct 12 '18 at 17:07

3 Answers3

144

?: 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.

Ether
  • 53,118
  • 13
  • 86
  • 159
Ben James
  • 121,135
  • 26
  • 193
  • 155
  • 1
    Thank you! I was confused when I saw them together like that "?:" and I searched but couldn't find anything on it. – JasonDavis Jan 28 '10 at 08:52
  • 3
    Hmm very cool. That makes `?:` equivalent to `||` in Javascript! – Paul May 07 '12 at 01:27
  • 4
    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. – MSpreij Feb 12 '14 at 16:00
  • @MSpreij 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!). – Demonslay335 Jun 26 '17 at 15:57
  • 1
    @MSpreij Actually, to further prove it isn't a terrible idea, PHP 7 introduced the [null coalescing operator](http://php.net/manual/en/migration70.new-features.php#migration70.new-features.null-coalesce-op) (I forgot about this myself) which does a similar thing and has the same general syntax - the difference being `isset()` vs truthy value though. – Demonslay335 Jun 26 '17 at 16:03
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.

Ether
  • 53,118
  • 13
  • 86
  • 159
Gumbo
  • 643,351
  • 109
  • 780
  • 844
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).

Boldewyn
  • 81,211
  • 44
  • 156
  • 212