0

[update] This isn't a duplicate question of "what's the difference between AND and &&" rather it's a question regarding the use of AND and the ternary operator. anyway i think i got my answer i didn't know that all operators have precedence which alex kindly pointed out. http://php.net/manual/en/language.operators.precedence.php

[original]

    echo TRUE && TRUE ? 'A' : 'B';
    // Outputs: A

    echo TRUE AND TRUE ? 'A' : 'B';
    // Outputs: 1

I know that the ternary operator syntax is

    condition ? true : false

and the difference between && and AND is evaluation precedence (of the condition)

e.g. TRUE AND TRUE && TRUE is evaluated at (TRUE AND (TRUE && TRUE))

so shouldn't using both AND and && yield the same return? is this a PHP bug? (im using ver5.5.23)

or should i just accept the fact that if i used AND in the condition it actually becomes excluded from the condition

e.g. TRUE AND TRUE ? 'A' : 'B' => (TRUE AND (TRUE ? 'A' : 'B'))

PK.
  • 2,471
  • 3
  • 24
  • 30
  • Yes, there is. `&&` has precedent over `AND` same goes for `||` and `OR`. See [this](http://stackoverflow.com/questions/2803321/and-vs-as-operator) – Andrei Jul 03 '15 at 11:07
  • you're missing brackets `echo (true and true) ? 'A' : 'B';` will always output A though since its not a proper conditional – Dave Jul 03 '15 at 11:10
  • i know && has precedence over AND but that's not the problem here... the problem is that the ternary operator itself has precedence over the AND, which to me is weird. – PK. Jul 03 '15 at 11:11
  • i know it will work if we put bracket to correct this... but the question is why when brackets are omitted the behaviour is not as expected. – PK. Jul 03 '15 at 11:13
  • You have `echo (TRUE && TRUE) ? 'A' : 'B';` and `echo TRUE AND (TRUE ? 'A' : 'B');`, if that helps. – Salman A Jul 03 '15 at 11:16

0 Answers0