1

I have a PHP echo function inside of a HTML link, but it isn't working. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this.

<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>">
  <img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" />
</a>
5
  • 4
    syntax error? <img src="<?php $image=troll/GrannyTroll.jpg?> Commented Jun 6, 2011 at 21:23
  • <a href="http://statuspics.likeoverload.com/<?php echo $picture; ?>"><img src="<?php $image=troll/GrannyTroll.jpg?>" width="100" height="94" /></a> Doesn't work. Commented Jun 6, 2011 at 21:25
  • yep because you have a syntax error there. just read the answer someone posted below .. or could you just explain what <?php $image=troll/GrannyTroll.jpg?> should do? Commented Jun 6, 2011 at 21:26
  • I want the file name from the <img src= to be put at the end of this link. http://statuspics.likeoverload.com/ Commented Jun 6, 2011 at 21:29
  • It seems kind of pointless to set a variable to a value ($image=troll/GrannyTroll.jpg) and then echo it out right away, unless the variable is set elsewhere. Commented Jun 6, 2011 at 21:33

2 Answers 2

3

Turn

<?php $image=troll/GrannyTroll.jpg?>

into

<?php echo "troll/GrannyTroll.jpg"; ?>

?

Or provide more details on what you are trying to achieve.

Also, you might consider urlencode-ing some of those URL parameters.

Edit:

So you might try setting the variable beforehand:

<?php $image = "troll/GrannyTroll.jpg"; ?>

<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>"><img src="<?php echo $picture; ?>" width="100" height="94" /></a>
Sign up to request clarification or add additional context in comments.

8 Comments

I want the file name from the <img src= to be put at the end of this link. http://statuspics.likeoverload.com/
+1 but I would prefer single quotes, if I really do not need double ones: <?php echo 'troll/GrannyTroll.jpg'; ?>
@Tadeck Just escape the single quotes
@smott I just realized this whole code makes no sense - we echo some constant string within some HTML ;) The result is always the same and does not require PHP. But yes, using single quotes when you do not require them is not only micro performance optimization, but also avoiding unexpected mistakes in the future, like adding some dollar signs. If you do not need it, why use it? KISS rule.
Thanks, but I don't know if what I am trying to do is possible. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this. Thank you for your help, however.
|
0

So now i understand what you are trying to do.

One error is that you didn't enclose $image=troll/GrannyTroll.jpg with quotes like this:

$image = 'troll/GrannyTroll.jpg';

The second error is that you do it in the wrong order, you have to define $image first, before you use it. That's what I believe you want to do:

<?php
    $image = "troll/GrannyTroll.jpg";
?>
<a href="http://statuspics.likeoverload.com/<?php echo $image; ?>"><img src="<?php echo $image; ?>" width="100" height="94"/></a>

1 Comment

Thanks, but I don't know if what I am trying to do is possible. I want to have an image location, defined in img src, be in part of the clickable link of the image. The page will have multiple images doing the same thing, so I am trying to use PHP to automate this. Thank you for your help, however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.