Search code examples
phphtmlattributeswhitespace

How do i add space between this php variables


So this works great when posted to the db. NameWeb Developer but what i wanted is Name Web Developer. am concatenating the value veriable i don't if is posible. below is the code.

            $spName .= $row['firstname'];
            $spPro = $row['profession'];

            $options .= '<option value='. $spName . '' . $spPro .'>' . $row['firstname'] . ' ' . $row['lastname'] . ' - ' . $row['profession'] . '</option>';
            // or i could also do it this way.                                                                                                              
            $options .= '<option value='. $row['firstname'] . '' . $row['profession'] .'>' . $row['firstname'] . ' ' . $row['lastname'] . ' - ' . $row['profession'] . '</option>';

But still not adding space between them.


Solution

  • You're generating invalid HTML. This:

    '<option value='. $spName . ' ' . $spPro .'>'
                                 ^--- WITH the added space
    

    Will produce something like this:

    <option value=SomeName SomeProfession>
    

    The browser has no way of knowing that these two values are part of the same attribute. In short, you forgot the quotes. You want to generate something like this:

    <option value="SomeName SomeProfession">
    

    So put the quotes in your code:

    '<option value="'. $spName . ' ' . $spPro .'">'
    

    In short, always look at the actual HTML that's in your web browser when debugging this. Don't just look at what ends up in the database somewhere downstream in the system, but look at the actual steps which lead to that downstream result. Invalid HTML is often fairly obvious to see when you look at the HTML itself.