Search code examples
htmlhrefstring-concatenation

How to append href strings with variables in html with PHP?


I have a list of href items as shown below:

<header class="less-header" role="banner">
    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a href="test.php?user=SORTENDDATE">SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
        <li><a href="test.php?user=SORTAZ">SORT A to Z</span></a></li>
    </ul>
</header>

Now I want to add a variable also to the href string so that it becomes:

<?php
$Custom = "APPS";
?>  

<header class="less-header" role="banner">
    <h1><a href="#">Grid Options</h1>
    <ul>
        <li><a href="test.php?user=SORTENDDATE&($Custom)">SORT by END DATE</span></a></li>
        <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
        <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
        <li><a href="test.php?user=SORTAZ&($Custom)">SORT A to Z</span></a></li>
    </ul>
</header>

How to append href strings with variables in html using php?

I tried this much:


Solution

  • Unless I'm missing the point, you almost have it, you just need to put the $Custom variable in php echo tags...

    <?php
    $Custom = "APPS";
    ?>
    
    <header class="less-header" role="banner">
        <h1><a href="#">Grid Options</h1>
        <ul>
            <li><a href="test.php?user=SORTENDDATE&<?= $Custom ?>">SORT by END DATE</span></a></li>
            <li><a href="test.php?user=SORTSTARTDATE">SORT by START DATE</span></a></li>
            <li><a href="test.php?user=SORTZA">SORT Z to A</span></a></li>
            <li><a href="test.php?user=SORTAZ&<?= $Custom ?>">SORT A to Z</span></a></li>
        </ul>
    </header>
    

    As Grace Lee noted, you have closing </span> tags without opening ones which you should look at correcting, but that isn't relevant to the question.