bɪˈɡɪnə
bɪˈɡɪnə

Reputation: 1085

How to put a link passing variable inside php echo

I have a link that passes variable to another page but I want to display that link only on some condition so how to place it inside php code.

<a href="page2.php?Id=<?php echo $Id; ?>">Product</a> 

<?php
echo '<a href="page2.php?Id=<?php echo $Id; ?>">Product</a>';
?>

This doesn't work.

Upvotes: 1

Views: 2658

Answers (4)

Chirag Senjaliya
Chirag Senjaliya

Reputation: 460

You dont have to print echo inside3 echo so u hv to do this way!

<?php
echo '<a href="page2.php?Id="'.$Id.'" ">Product</a>';
?>

Upvotes: 1

Niroj Adhikary
Niroj Adhikary

Reputation: 1835

Your echo field should be.

<a href="page2.php?Id=<?php echo $Id; ?>">Product</a> 

<?php
echo '<a href="page2.php?Id='.$Id.'">Product</a>';
?>

Upvotes: 1

Pupil
Pupil

Reputation: 23978

Simply, put your anchor link in if condition:

<?php
if (YOUR_CONDITION_HERE) {
  echo '<a href="page2.php?Id='.$Id.'">Product</a>';
}
?>

OR

<?php
if (YOUR_CONDITION_HERE) {
?>
<a href="page2.php?Id=<?php echo $Id;?>">Product</a>
<?php
}
?>

Above condition will display your link only when your condition is TRUE.

Otherwise, it will hide.

Upvotes: 1

Nirnae
Nirnae

Reputation: 1345

Your second line is incorrect why are you opening your php tags again while you're in a echo ?

Please try the following :

<?php
 echo '<a href="page2.php?Id='.$Id.'">Product</a>';
?>

Upvotes: 1

Related Questions