PHP新手。尽管在语法方面严格遵循了这本书,但在heredoc上还是遇到了一些令人沮丧的问题。下面的文本没有按应有的方式缩进。
<?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;
?>
基于"<!--"的存在,您正在创建一个html页面。
您看到了错误的问题,heredocs不会修改空白,但html会忽略它,除非您另有指定。要确认,只需查看页面来源,它将是您所期望的。
如果希望保留空白,请使用<pre>标签或更正确地使用真实标记
即
<pre>
This
is
3 lines
<pre>
或
This<br>
is<br>
3 lines<br>
或
<p>This<p>
<p>is</p>
<p>3 line</p>
将全部在3行上渲染。
w3空白引用