[php] Insert php variable in a href

I am planning to insert a PHP variable which holds the directory path for a file stored on my Windows machine. How can I include this variable in the a href tag inside my php script such that when the user clicks this link it should be redirected to that particular folder and file.

For ex: $folder_path = 'C:\docs\test\file1.txt';

Right now I have tried some different ways but with no success. I have also did some research on internet, but alas could not find a proper answer.

If any one has an idea would be thankful if it can be shared. Thanks

This question is related to php href

The answer is


in php

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

or

<a href="<?=$folder_path?>">Link text</a>;

or

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

Try using printf function or the concatination operator

http://php.net/manual/en/function.printf.php


You could try:

<a href="<?php echo $directory ?>">The link to the file</a>

Or for PHP 5.4+ (<?= is the PHP short echo tag):

<a href="<?= $directory ?>">The link to the file</a>

But your path is relative to the server, don't forget that.