Hertus Hertus - 4 months ago 11x
PHP Question

echo background image with a value

I echo out an image like that:

$newString = $thumbPre.'profilemain'.$thumbPost;
echo "<img src='http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error'>";

Now i want the image as a background-image, i tried it like that, but it doesn´t work:

echo '<div style="background-image:url('http://render-api-us.worldofwarcraft.com/static-render/us/" . $newString. "' alt='error');"></div>';


Firstly, remove alt='error' because background-image does not have an alt parameter, img does (you probably thought you could use that from your original code). In trying to use that, your background will not show up.

And your background won't show unless you have content inside that div. I've added Content as an example.

echo '<div style="background-image:url(\'http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.'\');">Content</div>';

You either have to escape the encapsulating quotes, or remove them altogether.

echo '<div style="background-image:url(http://render-api-us.worldofwarcraft.com/static-render/us/' . $newString.');">Content</div>';

Error reporting would have also thrown you a parse error such as:

Parse error: syntax error, unexpected '' alt='' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';'