Tiago
Tiago

Reputation: 4470

PHP echo resulting in jumbled up string

Basically I want to create a div with specific styling using PHP.

I have the following intended style saved up in a separate string for easy editing:

$bg = "background: url('./flags/" . $country[$id[0]]["iso"] . ".png')no-repeat center center fixed;";

And this is the echo that generates the div:

echo "<div class='flag' style='" . $bg . "'></div>";

When I run this code, the div does appear, but the style part is all jumbled up and weirdly formatted, like so:

<div class="flag" style="background:url(\" .="" flags="" hk.png\')no-repeat="" center="" fixed;'=""></div>

What is causing this problem?

Thanks in advance.

Upvotes: 0

Views: 78

Answers (1)

Nate
Nate

Reputation: 1360

I guess I'll repost this here, since it worked out:

In the $bg variable, you're encasing the url in single quotes -- but in your echo statement, you're doing the same thing for the style attribute. So when your $bg variable renders, it's closing the single quotes. I could be wrong, but I would try switching out the quotes in your $bg variable like this:

$bg = 'background: url("./flags ... etc ...

Upvotes: 1

Related Questions