dongtuwu8548
2018-05-31 06:01
浏览 95
已采纳

当我使用php echo时,为什么标签元素会自动关闭?

I was trying to make this code as below.

PHP code I wrote:

$i = 1;
<div class="sampleNumberIndex">
  <p> 
    <h2> <?php echo $i; ?></h2> 
  </p> 
</div>

Made code that I expected

<div class="sampleNumberIndex"> 
  <p> 
    <h2> 1 </h2> 
  </p> 
</div>

But php code made this.

<div class="sampleNumberIndex"> 
  <p> </p>
    <h2> 1 </h2> 
  <p> </p>
</div>

I'm using chrome, what did make it like that?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglin5770 2018-05-31 06:09
    已采纳

    That has nothing to do with PHP. It's just that html doesn't want you to wrap h2-tags in p-tags. You should rather use divs, regardless of what you are trying to accomplish here.

    打赏 评论
  • dqc18251 2018-05-31 06:21

    You are probably looking at Inspect Element section which repairs bad HTML codes. Real source codes are available at View page source section. Try Ctrl + U to view source code.

    打赏 评论
  • duanlu5055 2018-05-31 07:07

    It's not very clear from your question, but I suppose you're seeing the unexpected tags in the browser's DOM inspector. "Show Page Source" should show you the actual raw HTML your browser received.

    Edit: what @AliN11 said

    HTML infers tags when missing in content. In your case, the HTML browser knows that h2 can't appear in the content of a p element, hence it adds the </p> end-element tag before h2. Then after the h2 element, it encounters the </p> end-element tag, and inserts a <p> start-element tag before it, because none is open at the context position.

    The first insertion - that for the </p> end-element tag - is part of the regular parsing rules for HTML; omitting </p> is allowed according to the HTML specification. But the second insertion - that for the <p> start-element tag - is not, and is an effect of HTML recovery (repair) kicking in.

    I've explained HTML/SGML tag insertion in detail on the project page of my SGML software and the linked slides of a talk I gave about it last year.

    打赏 评论

相关推荐 更多相似问题