如何在 PHP 中使用 IF 语句来决定是否应该显示图像


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

我正在使用 wunderground.com API设置天气页面。无论如何,我想执行以下操作:

变量$weather等于Clear,我想显示一个图像。

我试过这个:

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

我需要做什么?

试试这个

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

您尝试的代码将呈现错误。在echo HTML 文本和任何字符串之前,您需要将它们放在引号内。

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

剩下的,没有什么可以改进的:)

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

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

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

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

很快。

对于有条件地渲染 html,我经常只使用 php 标签......

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