简体   繁体   English

PHP将html图像嵌入为链接

[英]PHP embed html image as a link

How do I go about using an image as a link in php? 如何在PHP中使用图像作为链接? I have never put two html elements together in one echo so it's kinda new for me. 我从来没有把两个html元素放在一个回声中,所以对我来说这有点新鲜。 Here's my code: 这是我的代码:

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 ';' PHP解析错误:语法错误,意外T_STRING,期待','或';' in /home6/dreamsm2/public_html/htmltest.php on line 11 在第11行的/home6/dreamsm2/public_html/htmltest.php中

Can anyone tell me what I'm doing wrong? 谁能告诉我我做错了什么? Any help would be appreciated. 任何帮助,将不胜感激。

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. 正如错误所暗示的那样,问题在于PHP解释器无法确定字符串应该从哪里开始和结束。 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. 使用\\"转义引号。在字符串周围使用'在字符串周围提供唯一的字符串分隔符,因此您可以在内部使用双引号。

Note, if you needed both single and double: 请注意,如果您需要单人和双人:

echo  '<a href="pageone.php" title="Andy\'s Link"><img src="homelogo.jpg"  /></a>';

You can also use ' instead of " for strings, eg 您也可以使用'而不是"作为字符串,例如

This works: echo '"Hello!"'; => "Hello!" 这有效: echo '"Hello!"'; => "Hello!" echo '"Hello!"'; => "Hello!"

This wont work: echo "'Hello'"; 这不起作用: echo "'Hello'";

简单这样做:

echo '<a href="page.php"><img src="Downloads_clip_image010.jpg" /></a>';

For WordPress 对于WordPress

<div class="floatLeft"> 
    <a href="http://trophydevelopers.com">
        <img src="<?php bloginfo('template_url'); ?>/images/powered-by.png">
    </a>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM