如何使用样式编写 php:""

How to write php using style:""

我想在样式标签中写 php 但我不知道怎么写。现在我被这个困住了:

echo"<div class='fill' style='background-image:url('upload/".$row['file1']."');'></div>";

但在浏览器中显示如下:

<div class="fill" style="background-image:url(" upload="" test.png');'=""></div>

有人可以帮助我吗?

这与PHP无关。

您有一个 HTML 属性值,您用 ' 个字符分隔。

您正在尝试使用其中的 ' 作为数据。

它终止属性值而不是表示引号。

将其替换为字符引用:&#039;

将您的 ' 换成您的 ",或者您可以转义引号

echo '<div class="fill" style="background-image:url(upload/' . $row['file1'] . ');"></div>';

问题是您使用 ' 作为 style 属性值周围和括号内 URL 周围的分隔符。第二个 ' 结束值。 URLs inside a url(...) style不需要引用,所以你可以写:

echo "<div class='fill' style='background-image:url(upload/".$row['file1'].");'></div>";

或使用反斜杠转义引号

echo"<div class='fill' style='background-image:url(\'upload/".$row['file1']."\');'></div>";