Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP embed html image as a link

Tags:

php

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:

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

Can anyone tell me what I'm doing wrong? Any help would be appreciated.

like image 225
Andy Chan Avatar asked May 31 '26 10:05

Andy Chan


2 Answers

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.

Note, if you needed both single and double:

echo  '<a href="pageone.php" title="Andy\'s Link"><img src="homelogo.jpg"  /></a>';
like image 116
sberry Avatar answered Jun 02 '26 03:06

sberry


You can also use ' instead of " for strings, e.g.

This works: echo '"Hello!"'; => "Hello!"

This wont work: echo "'Hello'";

like image 28
A Person Avatar answered Jun 02 '26 02:06

A Person



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!