我正在使用 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}