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 🙂

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!