Maybe I don't get the point (syntax) of the ternary operator.

($i !== $tot) ? (echo '') :  (echo ' colspan="2"');
if($i !== $tot) ? echo '' :  echo ' colspan="2"';

Both give me errors.

"Unexpected T_ECHO"

Can someone please explain to me why this doesnt work. Also, please make this work. 😉

    This works.

    echo ($i !== $tot) ? '' : ' colspan="2"';

    I'd still like an explaination as to why echo cannot be inside it.

    Thanks.

      I rather feel the difference then know for sure. a?b:c; operator should return b if a is true and c if a isn't true. Echo doesn't return any value (it returns void) so b in your first example can not evaluate to anything.
      Well, that's what I think...

        But echo is placed in both options.

        So either way, it should return something. Whether null or colspan="2".

        My question was why can't I put echo in the options. I know how it evaluates. 🙂

          No, I meant to say that echo is defined as
          void echo(string);
          So both in b and c it doesn't return anything. Even echo ' colspan="2"' returns void.

            Oh...that makes more sense. Not completely, but that's just cause echo returning void is new to me. I'll get used to it...or something.

            Thanks. 🙂

              No, it's not because it returns void, as I've tried it with putenv and other functions that return void and they work fine. You can get it to work using print() instead of echo.

              Remember that echo isn't actually a function, it's a language construct. I think that might have something to do with it.

              You can use

              mysql_query($q) or print("it's up the shoot, boss!");

              but you can't use echo in that case either.

              Maybe echo is the Michael Knight of PHP and can make a difference.

              Ahh - print is now actually not a funciton, but a language construct.

              echo is good because you can do things like

              echo 'lovely', $i, ' super', $b, ' smashing<br/>'
              ;

              which is quicker than using the dots for string concatenation, but you can't do that with print, maybe that's what's meddling with things.

              EDIT #2: Straight from the manual

              // Because echo is not a function, following code is invalid.
              ($some_var) ? echo('true'): echo('false');

              // However, the following examples will work:
              ($some_var) ? print('true'): print('false'); // print is a function
              echo $some_var ? 'true': 'false'; // changing the statement around

              ;

              But it also says in the manual under print

              print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

              So I think this: just treat it like a weird friend who you love, even though they're a complete freak.

                Hey Kudose,
                I know this is a bit off-topic, but do yourself a favor, and get out of the habit of using the Ternary at all, and write your code out.

                The Ternary operator is the least standard and least used method, and because of its syntax and limitations in usage; someone unfamiliar with it may have problems helping you if you need it, or worse yet, using your code if you published it, since they wouldn't know how to address it themselves. If you are writing the code for your own use only, then whatever, but if you plan on publishing, then avoid it. (Just my 2 cents)

                  Not to mention that many software houses forbid its use, in whatever languages they use. For one thing, that cuts down on the number of complaints by employees about their colleagues' code. The typing time saved is more than offset by loss of readability, maintainability, expandability, etc.

                  It's hilarious, too, to see someone post code with ternary clauses while trying to "help" a complete newbie.

                    It's hilarious, too, to see someone post code with ternary clauses while trying to "help" a complete newbie.

                    Yeah, especially when then the claim falls.. "This is much better coding"

                    J.

                      Out of using PHP for 2+ years, I have only used the ternary operator 2-3 times. I only picked it up because of a C++ class.

                      With the way this thread is going, I am going to stop using it all together.

                        never used it. Don't see the use. Prefer normal if statement.

                        J.

                          Got to admit using it, but only ever during echo statements for things like telling them if they're logged in or not.

                            Write a Reply...