-2

Here is the code:

$vote_up = '<a href="<?php amc_comment_vote("up") ?>">Vote Up</a>';

I am simply trying to have my PHP function insert a link that has a line of PHP as the href. But it does not work right. How can I do this?

Quick update: I got some good answers, however I'd like for the function to fire when clicked (hence the need for the <?php tags).

Greg L
  • 470
  • 1
  • 6
  • 20
  • It is similar, but I am asking how to insert a PHP link using a PHP function. If I was to place the html link with the PHP tags as href on my own this would function fine. So it is not a duplicate of the question you referenced. – Greg L Nov 17 '13 at 07:18
  • 1
    There are more duplicates like this, all explaining the most basic misunderstanding here: PHP does run server-side. You can't include/ship PHP code to the browser. Your browser only understands HTML and JS. It has no means to locate a PHP function, much less interpret the concept in a link tag. – mario Nov 17 '13 at 07:21
  • 1
    You are right, brain fart on my end. Thank you for clarifying, looking at the screen to long. Ill leave the question up only because my phrasing may be helpful to someone else searching for the same answer. Thank you – Greg L Nov 17 '13 at 07:30

2 Answers2

1

You're trying to use PHP tags inside the string context. They have no meaning and hence won't produce the output you need.

Simply use string concatenation:

$vote_up = '<a href="' . amc_comment_vote("up") . '">Vote Up</a>';

Or use sprintf() (bit more cleaner):

$vote_up = sprintf('<a href="%s">Vote Up</a>', amc_comment_vote("up"));
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • But that automatically fires the function, I want it only to fire when the link is clicked. – Greg L Nov 17 '13 at 07:12
  • you will have to use javascript or a link to make it call a different endpoint with the function, as the function is processed server-side. – Greg Nov 17 '13 at 07:15
1

this is also a possibility:

 $vote_up = "<a href='".amc_comment_vote("up")."'>Vote Up</a>";
Greg
  • 652
  • 6
  • 7