how can I assign a variable value to a href

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    how can I assign a variable value to a href

    Hi all,
    I have some results which I need to link with their original files.
    they are stored in variable $str
    how can I give a href a variable name ??
    code is below
    Code:
    <?php
    $str = "MS/2008-09/003/0112.ps ";
    print('<a href = "/$str" >Info</a>');
    ?>
    how can I assign a variable to a href ???
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Like so (you could use concatenation, but doing it in a value always confuses me with the quotes)

    Code:
    <?php
    $str = "MS/2008-09/003/0112.ps ";
    print("<a href = \"/{$str}\" >Info</a>");
    ?>
    Notice I have escaped the quotes within quotes with backslashes.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, AJD.

      If you used echo instead of print, you could do this:

      [code=php]
      echo '<a href="', $str, '">Info</a>';
      [/code]

      But you might want to consider doing this, instead:

      [code=php]
      echo '<a href="', htmlentities($s tr), '">Info</a>';
      [/code]

      Comment

      • ajd335
        New Member
        • Apr 2008
        • 123

        #4
        Hi All,
        Thanks for the solutions.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Originally posted by ajd335
          Hi All,
          Thanks for the solutions.
          Welcome. Glad we could help!

          Comment

          Working...