hakas
hakas

Reputation: 67

How to echo inside html tags

Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
 <a href="$link"><php? echo $text; ?></a> 

Why is this not printing out link and text assigned in the php code inside the html tags?

Upvotes: 2

Views: 9480

Answers (6)

Anu
Anu

Reputation: 1

Open PHP tags properly

You can embedd PHP inside tag as like :

<a href="<?php echo $link?>"><?php echo $text;?></a>

Upvotes: 0

user3419844
user3419844

Reputation:

Use sprint

<?php
$text ='Click here';
$link = 'http://www.google.com';
echo sprintf(" <a href="%s">%s</a>", $link, $text);
?>

Upvotes: 2

Martyn Shutt
Martyn Shutt

Reputation: 1706

If you'll always be running your code in PHP 5.4+, you could use short echo tags;

<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
 <a href="<?= $link ?>"><?= $text ?></a> 

Looks a little neater in my opinion, but it's a matter of preference, and short echo tags aren't on by default in earlier versions of PHP, so I wouldn't recommend it if your code is ever going to run on server with PHP versions below 5.4

Upvotes: 5

Arun Kumar
Arun Kumar

Reputation: 1667

Another way

Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
echo '<a href="'.$link.'">'.$text.'</a>';
?>

Upvotes: 2

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2807

use the below code

<a href="<?php echo $link ?>"><?php echo $text; ?></a> 

For the server to interpret your php you need to close all your php code inside the <?php ?> tags and then echo that variable

Upvotes: 1

Harshit
Harshit

Reputation: 5157

Use this

Embedding php inside html </p>
<?php
$text ='Click here';
$link = 'http://www.google.com';
?>
 <a href="<?php echo $link; ?>"><?php echo $text; ?></a> 

It is <?php not <php?

Upvotes: 1

Related Questions