What is ?: in PHP 5.3?

47,810

Solution 1

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

Solution 2

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.

Solution 3

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).

Share:
47,810

Related videos on Youtube

JasonDavis
Author by

JasonDavis

PHP/MySQL is my flavor of choice however more recently JavaScript is really becoming something I enjoy developing with! Writing code since 2000' Currently working heavily with SugarCRM + Launching my Web Dev company ApolloWebStudio.com "Premature optimization is not the root of all evil, lack of proper planning is the root of all evil." Twitter: @JasonDavisFL Work: Apollo Web Studio - https://www.apollowebstudio.com Some of my Web Dev skills, self rated... +------------+--------+------+--------------+ | Skill | Expert | Good | Intermediate | +------------+--------+------+--------------+ | PHP | X | | | +------------+--------+------+--------------+ | MySQL | X | | | +------------+--------+------+--------------+ | Javascript | X | | +------------+--------+------+--------------+ | jQuery | X | | +------------+--------+------+--------------+ | CSS+CSS3 | X | | +------------+--------+------+--------------+ | HTML+HTML5 | X | | | +------------+--------+------+--------------+ | Photoshop | | X | | +------------+--------+------+--------------+ | Web Dev | X | | | +------------+--------+------+--------------+ | SugarCRM | X | | | +------------+--------+------+--------------+ | Magento | | X | | +------------+--------+------+--------------+ | WordPress | X | | | +------------+--------+------+--------------+ | SEO | X | | | +------------+--------+------+--------------+ | Marketing | X | | | +------------+--------+------+--------------+ |Social Media| X | | | +------------+--------+------+--------------+

Updated on December 15, 2020

Comments

  • JasonDavis
    JasonDavis over 3 years

    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?

    • Gordon
      Gordon over 14 years
    • JasonDavis
      JasonDavis over 14 years
      @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 "?:"
    • Dhir Pratap
      Dhir Pratap over 14 years
      @Gordon those are only the longer forms. @jasondavis, you have the : and ? backwards in the question title.
    • Gordon
      Gordon over 14 years
      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.
    • dwenaus
      dwenaus about 10 years
      this is not a duplicate.
    • SaidbakR
      SaidbakR over 9 years
      It is not a duplicate for the considered question. This question is meant by PHP 5.3
    • Sz.
      Sz. over 5 years
      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. ;)
  • JasonDavis
    JasonDavis over 14 years
    Thank you! I was confused when I saw them together like that "?:" and I searched but couldn't find anything on it.
  • Paul
    Paul about 12 years
    Hmm very cool. That makes ?: equivalent to || in Javascript!
  • MSpreij
    MSpreij over 10 years
    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.
  • Demonslay335
    Demonslay335 almost 7 years
    @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
    Demonslay335 almost 7 years
    @MSpreij 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.