Search code examples
phpsimplexml

Using PHP to set a backup (alternate) image


I'm looking to set a "backup" image as an alternate to my PHP generated image (pulling filename from SimpleXML)

<div style="background-image:url('//url.com/images/<?php echo $xml->IMGNODE; ?>.png');"></div>

Basically, if the above PHP code returns no value, I'd like to set it to a predetermined image filename rather than just showing nothing.


Solution

  • <div style="background-image:url('//url.com/images/<?= isset($xml->IMGNODE) ? $xml->IMGNODE : 'default_image' ?>.png');"></div>