Very Confused! Can You Work This Out?

Discussion in 'PHP' started by Masterful, Feb 17, 2009.

  1. #1
    The tiny piece of code below means, if the alias of a person is unavailable in the database, echo the person's name. Otherwise, echo the alias.

    echo (empty($row['alias']) ? $row['name'] : $row['alias']);
    PHP:
    If the alias is going to be echoed, I want it to be echoed in bold, red font, so I used a span tag and added a CSS class:

    echo (empty($row['alias']) ? $row['name'] : "Alias is: <span class=\"boldred\">" . $row['alias']) . "</span>";
    PHP:
    It worked. However, when it echoes the name and not the alias, my HTML doesn't validate, because it also echoes the closing span tag.

    In other words, it either echoes this

    Alias is: Alias

    or this

    Name</span>

    (of course, you can't see the span tag). Anyone know how I can fix this?
     
    Masterful, Feb 17, 2009 IP
  2. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    You have your closing bracket for echo in the wrong spot.
    It should be
    echo (empty($row['alias']) ? $row['name'] : "Alias is: <span class=\"boldred\">" . $row['alias'] . "</span>");
    PHP:
     
    Jezz, Feb 17, 2009 IP
    Masterful likes this.
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    Thanks, Jezz. It worked!

    One reputation point for you! ;)
     
    Masterful, Feb 17, 2009 IP
  4. Jezz

    Jezz Active Member

    Messages:
    208
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #4
    That's good to hear, Thankyou for the rep :)
     
    Jezz, Feb 18, 2009 IP