douchui4815
2015-06-26 11:44
浏览 140
已采纳

HTML / PHP:我们如何将PHP变量插入到html的锚标签中?

I have a .php page which has a lot of HTML parts in it. I am running a FOR loop and for each value in FOR loop, I want to pass a PHP variable in anchor tag which is inside the loop.

I have tried this:

for($i =0; $i<5 ; $i++)
{ 
<a href = "Test.html?ID= <?php $i ?> > Sample text </a>
}

But it isn't working.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • doukao2180 2015-06-26 11:50
    已采纳

    Any number of ways. The point is to not mix the HTML with the PHP, keep them separately parse-able. Something like this:

    for($i =0; $i<5 ; $i++)
    { 
        echo '<a href="Test.html?ID=' . $i . '"> Sample text </a>';
    }
    

    (In this example all of the code is PHP, and the HTML is a string that's echo-ed to the output.)

    Or this:

    for($i =0; $i<5 ; $i++)
    {
    ?>
        <a href="Test.html?ID=<?php echo $i; ?>"> Sample text </a>
    <?php
    }
    

    (In this example the PHP code is wrapped in <?php ?> tags and the HTML is kept outside those tags.)

    As long as you keep PHP code in the <?php ?> tags and HTML out of those tags, the parsers will know the difference.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dopuzf0898 2015-06-26 11:47

    Your syntax is wrong and you are not outputting anything, replace it with:

    for($i =0; $i<5 ; $i++)
    { 
      echo '<a href="Test.html?ID='.$i.'>Sample text</a>';
    }
    
    评论
    解决 无用
    打赏 举报
  • duanbichenhe1968 2015-06-26 11:48

    The html has to be included in echo while using in php

     <?php
        for($i =0; $i<5 ; $i++)
        { 
           echo '<a href="Test.html?ID='.$i.'>Sample text</a>';
        }
        ?>
    
    评论
    解决 无用
    打赏 举报
  • duanlingzei0170 2015-06-26 11:58
    for($i =0; $i<5 ; $i++)
    { 
          <a href = "Test.html?ID= <?php $i ?> > Sample text </a>
    }
    

    If above lines are code u have tried, its not in proper format! u running loop in php! so u cannot put html tags directly in php file! use echo to display html tags!

    for($i=0; $i<5; $i++)
    {
           echo "<a href=test.html?id=$i>Click here</a>";
    
    }
    

    Hope it was helpful!

    评论
    解决 无用
    打赏 举报
  • drwiupraq047311240 2015-06-26 18:23

    Try this.

    for($i=0; $i<5; $i++)
    {
        echo "<a href=\"Test.html?id='.$i.'\">Click here</a>";
    }
    
    评论
    解决 无用
    打赏 举报
  • duangao7133 2016-09-07 08:15
    for($i =0; $i<5 ; $i++)
    {
    ?>
    <a href="Test.html?ID=<?php echo $i; ?>"> Sample text </a>
    <?php
     }  
     ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题