我可以覆盖PHP内置函数echo()吗? [英] Can I override the PHP built-in function echo()?

查看:182
本文介绍了我可以覆盖PHP内置函数echo()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近查看了我的源代码,这真是一团糟.

I recently looked at my source code and it was a real mess.

我的php来源:

echo '<h1>Rar<h1>';
echo '<span>Rar</span>';
echo '<p>Rar</p>';

,当我查看呈现页面的浏览器源代码时:

and when I view the browser source for the page rendered:

<h1>Rar</h1><span>Rar</span><p>Rar</p>

有没有办法让我覆盖echo,以便每个输出都以换行符结尾,例如

is there a way for me to override echo so that every output would end with a newline, something like

function echo($string)
{
 echo $string . "\r\n";
}

推荐答案

不是函数,而是语言声明.无法重新定义.如果您要美化您的输出标记,请查看 Tidy .

echo is not a function, but a language statement. It cannot be redefined. If you are looking to prettify your output markup, have look at Tidy.

您可以 可以使用IDE的搜索/替换方法,并将所有echo语句替换为echo PHP_EOL,.这将在任何输出之前附加操作系统特定的换行符.请注意PHP_EOL之后的逗号,因为它很重要.

What you could do, is use your IDE's search/replace method and replace all echo statements with echo PHP_EOL,. This would append the OS specific newline char(s) before any output. Note the comma after PHP_EOL as it is important.

您可以使用echo输出多个值,如下所示:

You can output several values with echo like this:

echo 'one', $foo, PHP_EOL,
     'two', $bar, PHP_EOL;

因此无需在每行上写echo.

so there is no need to write echo on each line.

但是,我同意建议使用更专用的方法来分隔内容和布局的任何人,例如使用模板视图或HereDoc.

However, I agree with anyone who suggested using a more dedicated approach to separate content and layout e.g. using template views or HereDoc.

此外,具有漂亮的标记几乎没有什么好处.如果您使用诸如Firebug之类的工具来检查HTML,则无论标记实际上是一团糟,您都将拥有正确格式的标记.此外,在有很多访问者的网站上,您经常会发现标记变小了,这与您尝试做的相反,仅仅是因为所有这些换行符和标签都增加了页面的重量,这导致速度变慢页面加载和增加的流量成本.

In additon, there is very little gain in having pretty markup. If you are using tools like Firebug to inspect the HTML, you will have properly formatted markup regardless of the mess the markup really is. Moreover, on sites with a lot of visitors, you'll often find the markup minified, which is the opposite of what you are trying to do, simply because all these newlines and tabs add to the weight of the page, which leads to slower page loads and increased traffic cost.

这篇关于我可以覆盖PHP内置函数echo()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆