#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Ternary operator not working in echo


    My code

    PHP Code:
    echo "         <a href='?OB=name&oba='" . ($order == 'DESC' 'ASC' 'DESC') . "><img src='images/name.gif'></a>"
    Produces a link for

    ?OB=name&oba=

    Why doesn't it return the DESC or ASC?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,230
    Rep Power
    593
    I think it has to do with operator precedence since you are using strings. Try this:
    PHP Code:
    echo "         <a href='?OB=name&oba='" . (($order == 'DESC') ? 'ASC' 'DESC') . "><img src='images/name.gif'></a>"
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,907
    Rep Power
    1045
    Hi,

    no, those paranthesis are unnecessary, since "==" already has a higher precedence than the ternary operator.

    Perceptus, are you sure that's the exact output of the "echo" statement? Because what the statement should and does output (on my machine) is

    Code:
    <a href='?OB=name&oba='DESC><img src='images/name.gif'></a>
    Maybe it's because of the invalid single quote after the "oba=" that you don't see the full URL.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0
    Thanks Jacques1,

    New code works

    PHP Code:
    echo "         <a href='?OB=name&oba=" . ($order == 'DESC' 'ASC' 'DESC') . "'><img src='images/name.gif'></a>"

IMN logo majestic logo threadwatch logo seochat tools logo