Results 1 to 6 of 6

Thread: Operator '?'

  1. #1
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default Operator '?'

    Is there any operator that has '?' sign in php? If yes, then what is that for?

    I am trying to understand some source code, and the statement is:

    Code:
    $app_root_path = (defined('APP_ROOT_PATH')) ? APP_ROOT_PATH : './';
    Can you also explain the statement completely and what it does

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    It is called the ternary operator.

    PHP Code:
    // something to evaluate ? 'value if true' : 'value if false';

    $t 'A';
    //$t = 'B';
    echo preg_match('/A/'$t) ? 'match' 'no match'
    Used inside the echo statement above, it says: Depending on whether or not preg_match() is true or false (1 or 0), echo match or no match. If preg_match() returns 1, the word 'match' is echoed. If preg_match() returns 0, the words 'no match' will be echoed.

    Used inside your example, it says, if the constant APP_ROOT_PATH has been defined, set the $app_root_path variable equal to the value of the APP_ROOT_PATH constant. If APP_ROOT_PATH has not been defined, set the variable to './' Since the function defined() returns true or false, the ternary operator (?) can either use the first value after it if true, or the second, if false.
    Last edited by JasonDFR; 04-02-2009 at 06:25 AM.

  3. The Following User Says Thank You to JasonDFR For This Useful Post:

    borris83 (04-02-2009)

  4. #3
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Terenary operator can be thought of as a simple if else construct:

    The syntax:

    [your_variable = ] expression ? true part : false part;

    If your expression is evaluated to true then it will execute the true part or the false part.

  5. The Following User Says Thank You to codeexploiter For This Useful Post:

    borris83 (04-02-2009)

  6. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    More importantly, it returns the true or false part. Unlike the if statement, the ternary conditional operator forms an expression.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. The Following User Says Thank You to Twey For This Useful Post:

    borris83 (04-02-2009)

  8. #5
    Join Date
    Mar 2009
    Location
    Chennai, India
    Posts
    77
    Thanks
    16
    Thanked 7 Times in 6 Posts

    Default

    Thank you all, Very informative... I searched for it in php.net... I don't know what keyword to use, and hence I searched for 'operators', but it wasn't listed there...

    Cool! I never thought that I could write an 'IF ELSE' statement this way

  9. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's on the comparison operators page, at the bottom of the article.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •