使用带有 php 变量的 img 源代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/877406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use Img source with php variable?
提问by Sam152
i have the following code in my detailansicht.php:
我的 detailansicht.php 中有以下代码:
<div class="file_description_box">
<b>Beschreibung:</b><br /><br />
<?php
if(!empty($beschreibung))
echo '<div align="center">';
echo '<img src="$coverlink" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
**else**
echo '<i>Keine Beschreibung vorhanden</i>';
?>
</div>
but i think there has to be a mistake in the img tag. everytime i trie to open the page, he shows me an error: "Parse error: syntax error, unexpected T_ELSE in bla/bla/include/detailansicht.php on line 123" (Line 123 is the else marked bold). I tried several methods but i always get this error. It would be nice, if someone could help me with this.
但我认为 img 标签中一定有错误。每次我尝试打开页面时,他都会向我显示一个错误:“解析错误:语法错误,bla/bla/include/detailansicht.php 中第 123 行意外的 T_ELSE”(第 123 行是其他标记为粗体的)。我尝试了几种方法,但总是收到此错误。如果有人能帮我解决这个问题,那就太好了。
-sTarbuZz
-sTarbuZz
回答by Sam152
You are missing some curly brackets and your PHP variable wasn't embedded property. Your code should look like this:
您缺少一些大括号,并且您的 PHP 变量不是嵌入式属性。您的代码应如下所示:
<div class="file_description_box">
<b>Beschreibung:</b><br /><br />
<?php
if(!empty($beschreibung)){
echo '<div align="center">';
echo '<img src="'.$coverlink.'" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
}else{
echo '<i>Keine Beschreibung vorhanden</i>';
}
?>
</div>
Just on a side note it really doesn't matter what you use PHP for, if there was a fault with the image tag and it wasn't being displayed properly it would generally be a HTML problem assuming you have outputted it correctly.
顺便提一下,您使用 PHP 的目的实际上并不重要,如果图像标签有问题并且它没有正确显示,那么假设您已正确输出它通常是 HTML 问题。
In this case it was a PHP problem because there were a few errors in the code, it hasn't really got anything to do with the image tag.
在这种情况下,这是一个 PHP 问题,因为代码中有一些错误,它与图像标签没有任何关系。
回答by Peter Perhá?
You are missing curly braces:
你缺少花括号:
if () {
...
} else {
...
}
so your PHP is not syntactically correct and will not be parsed by PHP hypertext pre-processor.
所以你的 PHP 在语法上不正确,不会被 PHP 超文本预处理器解析。
回答by karim79
Try using curly braces:
尝试使用花括号:
<?php
if(!empty($beschreibung)) {
echo '<div align="center">';
echo '<img src="$coverlink" alt="Cover">';
echo '</div><br />';
echo format_content($beschreibung);
} else {
echo '<i>Keine Beschreibung vorhanden</i>';
}
?>
回答by user108732
use
用
echo '<img src="', $coverlink', " alt="Cover">';
PHP variables inside in single quote won't be evaluated
单引号中的 PHP 变量不会被评估
回答by rslite
Yep, you're missing the curly braces. Simply formatting the code nicely with tabs won't make it a block.
是的,你缺少花括号。简单地用选项卡很好地格式化代码不会使它成为一个块。
Also you missed the ending of the img tag (/>), but that's unrelated to your question.
您也错过了 img 标签 (/>) 的结尾,但这与您的问题无关。
回答by Kris
I would go for the alternative syntaxwhich i feel is easier on the eyes intermixed with html:
我会选择另一种语法,我觉得在与 html 混合的眼睛上更容易:
<? if(!empty($beschreibung)) : ?>
<div align="center">
<img src="<?= $coverlink; ?>" alt="Cover">
</div><br />
<?= format_content($beschreibung); ?>
<? else : ?>
<i>Keine Beschreibung vorhanden</i>
<? endif ; ?>
PS: i am not advocating putting logic inside the markup, just noting it can be done.
PS:我不提倡在标记中放置逻辑,只是指出它可以做到。
edit: fixed syntax error (; after endif instead of :)
编辑:修复语法错误(; 在 endif 之后而不是 :)
回答by x4tje
<?php
if(!empty($beschreibung)) {
echo "<div align=\"center\">";
echo "<img src=\"$coverlink\" alt=\"Cover\">";
echo "</div><br />";
echo format_content($beschreibung);
} else {
echo "<i>Keine Beschreibung vorhanden</i>";
}
?>
回答by x4tje
thanks for all your answers... I'm trying Kris' method now...
感谢您的所有回答...我现在正在尝试 Kris 的方法...
And the thing with the brackets... i don't know how this could happen... i didn't write the original script (i just added the part with the cover) and i didn't recognize that there were missing brackets, because $beschreibung is never empty and i think php ignores the if and else if the brackets aare missing...
还有带括号的东西……我不知道这是怎么发生的……我没有写原始脚本(我只是添加了带有封面的部分)而且我没有意识到缺少括号,因为 $beschreibung 永远不会为空,我认为 php 会忽略 if 和 else 如果缺少括号 a...

