Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.

Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.

$link_address = '#'; echo '<a href="$link_address">Link</a>'; 
like image 979
jasonbradberry Avatar asked Sep 20 '13 12:09

jasonbradberry


People also ask

How do you pass a value into an href?

You need add quotes for your href , besides, you also need to use urlencode to encode the variable. . '">$compname</a>" ?? The last " should be a ' instead.

How can add URL in PHP code?

PHP link() Function$linkname = "mylink"; link($target, $linkname);

How do you echo an HTML variable?

'</span>'; Additionally, you can do it inline by escaping the double quotes: echo "<span class=\"label-$variable\">$variable</span>"; Using double quotes also allows for special characters such as \n and \t .

What is HREF in PHP?

The href attribute specifies the URL of the page the link goes to.


2 Answers

Try like

HTML in PHP :

echo "<a href='".$link_address."'>Link</a>"; 

Or even you can try like

echo "<a href='$link_address'>Link</a>"; 

Or you can use PHP in HTML like

PHP in HTML :

<a href="<?php echo $link_address;?>"> Link </a> 
like image 191
Gautam3164 Avatar answered Sep 23 '22 18:09

Gautam3164


you can either use

echo '<a href="'.$link_address.'">Link</a>'; 

or

echo "<a href=\"$link_address\">Link</a>'; 

if you use double quotes you can insert the variable into the string and it will be parsed.

like image 31
Davide De Santis Avatar answered Sep 19 '22 18:09

Davide De Santis