dongou0524 2012-03-06 11:22
浏览 40

heredoc没有缩进或正常工作

new to PHP. Just having some frustrating problems with heredoc, despite following the book to the letter on syntax. The below text isn't indented as it should be.

<?php

$text="Mike's";

echo <<<_END

<!--END is just like double quoteing a var..
You can use single/double quotes without having to escape them first; inside 
END. The last _END tag, has to be on the start of new line with nothing allowed
to procede it, not even whitespace-->

This is the $text 'first line'.

This is the $text 'second line'.

This is the $text 'third line'.
_END;
?>
  • 写回答

1条回答 默认 最新

  • donglinxi1467 2012-03-06 11:30
    关注

    Based on the presence of "<!--" - you are creating a html page.

    You're looking at the wrong problem, heredocs don't modify whitespace - but html ignores it unless you specify otherwise. To confirm just look at the page source, it'll be what you are expecting.

    If you want whitespace to be preserved - use a <pre> tag or more correctly use real markup

    i.e.

    <pre>
    This
    is
    3 lines
    <pre>
    

    or

    This<br>
    is<br>
    3 lines<br>
    

    or

    <p>This<p>
    <p>is</p>
    <p>3 line</p>
    

    will all render on 3 lines.

    w3 whitespace reference

    评论

报告相同问题?