July 2, 2012
How do I use an IF statement in PHP to decide if I should show an image or not?
Question by Chris Danger Gillett
I’m setting up a weather page using the wunderground.com API. Anyway, I want to do the following:
Of the variable $weather
is equal to Clear
, I want to display an image.
I’ve tried this:
if ($weather=="Clear") echo <img src="http://example.org/clear.gif" alt="Clear"/>
What do I need to do instead?
Answer by Ziumin
Try this
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';
Answer by Starx
The code you tried will render ERROR. You need to place the HTML text and any string within quotes, before you echo
them.
if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>'
Remaining, there is nothing else to improve 🙂