PHP模板中的换行奇怪

时间:2010-08-26 04:43:43

标签: php templates

我多年来一直在处理这个问题,但是我从来没有调查过它。但我一直在想。

在PHP模板中,如果我运行它:

<p>
<?php echo 'Between "?>" and "</p>" is one linebreak.'; ?>
</p>

<p>
<?php echo 'Between "?>" and "</p>" is one space, then one linebreak.'; ?> 
</p>

<p>
<?php echo 'Between "?>" and "</p>" is two linebreaks.'; ?>

</p>

这是输出:

<p>
Between "?>" and "</p>" is one linebreak.</p>

<p>
Between "?>" and "</p>" is one space, then one linebreak. 
</p>

<p>
Between "?>" and "</p>" is two linebreaks.
</p>

我不明白这种行为。为什么忽略单个换行符就好像它不存在(如第一个例子中那样)?

1 个答案:

答案 0 :(得分:5)

如果在解除关闭?>之后立即出现换行符,则PHP解释器会忽略换行符。这是为了与文本编辑器兼容,文本编辑器总是在文件的最后一行添加换行符。

PHP手册herehere中简要提到了这一点。

如果在换行符之前有一个空格,那么它就像正常一样输出,只有在换行之后它才会忽略它。