Hi everyone,

Some how, I have managed to program a number of languages and never in my life seen this sort of syntax:

echo ((true ? 'true' : 'false') ? 't' : 'f');

until recently... I was wondering if anyone here could break this syntax down and explain how it conceptually works, because I have been googling it and am not 100% clear on how to construct something like this myself, and when it's the most useful thing to do...

Thank you.

-patrick

    That's actually an example of a rather obfuscated ternary sequence, but it's roughly:

    (condition) : default : other;
    

    I use it a lot when I'm writing libraries or functions that would normally require an if/else statement.

    if ($action == true) {
       $do_this = 1;
    }
    else {
       $do_this = 0;
    }
    // is the same as
    ($action == true) ? $do_this = 1 : $do_this = 0;
    // if that actually works, I'm not sure :D  I just started working with the ternary operator recently
    

      It's very similar to an IF() construct in SQL:

      SELECT IF(IF(true, 'true', 'false'), 't', 'f');
      

      Basically, if the expression to the left of the "?" evaluates as true, then use the expression following the "?", else use the expression following the ":". The particular example you provided is a bit meaningless, since the inner ternary expression will always return the string 'true', which since it is a non-empty string which is not "0", will cause the 't' to be echoed.

      A common usage is in same-page form/form-handlers, where you want to pre-fill the fields if the values have already been submitted, typically when there was a validation error:

      <input type="text" name="sample" value="<?php
      echo (isset($_POST['sample'])) ? htmlentities($_POST['sample'], ENT_QUOTES) : '';
      ?>" />
      

        Some how, I have managed to program a number of languages and never in my life seen this sort of syntax:

        C, C++ and Java are some common programming languages that have such a language feature.

        Horizon88's example is better written as:

        $do_this = $action ? 1 : 0;

          And in case you haven't come across it yet... here is a link to the manual page discussing this syntax.

            For completeness, in visual basic flavors the equivalent function is IIF(statement, truepart, falsepart)

            Note the two I's in IIF rather than one I

              Not to be a stickler, but that first example will always echo "t" since 'false' is a string that is not empty which evaluates to a soft "true"....

                laserlight wrote:

                C, C++ and Java are some common programming languages that have such a language feature.

                Indeed, pretty much any language that has C in its ancestry, or has a C-founded syntax (and that probably covers almost all of the major languages in use), is likely to include it. Perl, JavaScript, and C# are three more that include the same "?:" conditional operator (which is its proper name), and even awk inherited it back in 1978 (which is how Perl got it). Python picked it up in 2006 (though with different symbols and with the operands in a different order: "B if A else C" instead of "A ? B : C").

                  Write a Reply...