php PHP嵌入html图像作为链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10152099/
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
PHP embed html image as a link
提问by Andy Chan
How do I go about using an image as a link in php? I have never put two html elements together in one echo so it's kinda new for me. Here's my code:
如何在 php 中使用图像作为链接?我从来没有把两个 html 元素放在一个 echo 中,所以这对我来说有点新。这是我的代码:
htmltest.php
htmltest.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?
require("includes/conn.php"); //link to the database
?>
<html>
<title>HTML with PHP</title>
<body>
<?php
echo "<a href="pageone.php"><img src="homelogo.jpg" /></a>";
?>
</body>
</html>
That's my code. I get the following error:
那是我的代码。我收到以下错误:
PHP Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home6/dreamsm2/public_html/htmltest.php on line 11
PHP 解析错误:语法错误,意外的 T_STRING,需要 ',' 或 ';' 在第 11 行的 /home6/dreamsm2/public_html/htmltest.php
Can anyone tell me what I'm doing wrong? Any help would be appreciated.
谁能告诉我我做错了什么?任何帮助,将不胜感激。
回答by sberry
Change the line to:
将该行更改为:
echo '<a href="pageone.php"><img src="homelogo.jpg" /></a>';
OR
或者
echo "<a href=\"pageone.php\"><img src=\"homelogo.jpg\" /></a>";
The problem, as the error somewhat suggests, is that the PHP interpreter can't figure out where your string is supposed to start and end. Using \"escapes the quotes. Using 'around the string gives a unique string delimiter around the string, so you are free to use double quotes inside.
正如错误所暗示的那样,问题在于 PHP 解释器无法确定您的字符串应该在哪里开始和结束。使用\"转义引号。'在字符串周围使用会在字符串周围提供一个唯一的字符串分隔符,因此您可以在其中自由使用双引号。
Note, if you needed both single and double:
请注意,如果您需要单人和双人:
echo '<a href="pageone.php" title="Andy\'s Link"><img src="homelogo.jpg" /></a>';
回答by A Person
You can also use 'instead of "for strings, e.g.
您也可以使用'代替"字符串,例如
This works: echo '"Hello!"'; => "Hello!"
这有效: echo '"Hello!"'; => "Hello!"
This wont work: echo "'Hello'";
这行不通: echo "'Hello'";
回答by Faizan Qadri
SIMPLY DO THIS:
只需这样做:
echo '<a href="page.php"><img src="Downloads_clip_image010.jpg" /></a>';
回答by Trophy Developers U CO. Ltd
For WordPress
对于 WordPress
<div class="floatLeft">
<a href="http://trophydevelopers.com">
<img src="<?php bloginfo('template_url'); ?>/images/powered-by.png">
</a>
</div>

