Chris Danger Gillett
Chris Danger Gillett

Reputation: 127

How do I use an IF statement in PHP to decide if I should show an image or not?

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?

Upvotes: 0

Views: 109

Answers (6)

iblue
iblue

Reputation: 30404

For conditionally rendering html, I often just use the php tags...

if($weather === "Clear") {?>
  <img src="http://example.org/clear.gif" alt="Clear"/>
<?php}

Upvotes: 0

loler
loler

Reputation: 2587

if ($weather=="Clear")  echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";

OR

if ($weather==="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';

OR

if ($weather==="Clear") echo <<<ABC
<img src="http://example.org/clear.gif" alt="Clear"/>
ABC;

andsoon.

Upvotes: 0

Starx
Starx

Reputation: 78971

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 :)

Upvotes: 2

Rab
Rab

Reputation: 35572

 echo '<img src="http://example.org/clear.gif" alt="Clear"/>';

OR

 echo "<img src='http://example.org/clear.gif' alt='Clear' />";

Upvotes: 1

Nir Alfasi
Nir Alfasi

Reputation: 53525

if ($weather==="Clear") 
  echo "<img src=\"http://example.org/clear.gif\" alt=\"Clear\"/>";

Upvotes: 1

Ziumin
Ziumin

Reputation: 4860

Try this

if ($weather=="Clear") echo '<img src="http://example.org/clear.gif" alt="Clear"/>';

Upvotes: 4

Related Questions