我可以覆盖PHP内置函数echo()吗?

时间:2023-01-15 20:51:27

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

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

my php source:

我的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>

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

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

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

6 个解决方案

#1


10  

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.

echo不是一个函数,而是一个语言语句。它无法重新定义。如果你想要美化你的输出标记,请看看Tidy。


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.

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

You can output several values with echo like this:

您可以使用echo输出多个值:

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

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

所以不需要在每一行上写回声。

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

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

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.

另外,获得漂亮标记的收益很少。如果您使用Firebug之类的工具来检查HTML,那么无论标记确实存在多么混乱,您都将获得格式正确的标记。此外,在拥有大量访问者的网站上,您经常会发现标记缩小,这与您尝试执行的操作相反,只是因为所有这些换行符和标签都会增加页面的重量,从而导致速度变慢页面加载和增加的流量成本。

#2


3  

You have various possibilities to output HTML.

您有各种输出HTML的可能性。

You can use the heredoc syntax:

您可以使用heredoc语法:

$html = <<<EOF
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
EOF
echo $hml;

Or (what is way better in my opinion), separate HTML from PHP. E.g. put all the PHP logic in the top of the file and the HTML after the PHP block:

或者(在我看来哪种方式更好),将PHP与PHP分开。例如。将所有PHP逻辑放在文件顶部,并在PHP块之后放置HTML:

<?php
   // all your PHP goes here
   $foo = 'bar'
?>
<!-- HTML comes here -->
<html>
  <body>
    <div>Hello <?php echo $foo; ?> </div>
  </body>
</html>

Variables can be printed as shown above. But these variables don't contain HTML.

可以如上所示打印变量。但是这些变量不包含HTML。

When you have to output HTML based on a condition, you can use the alternative syntax for control statements:

当您必须根据条件输出HTML时,您可以使用控制语句的替代语法:

<?php if($some_condition): ?>
    <h1>Rar<h1>
    <span>Rar</span>
    <p>Rar</p>
<?php endif ?>

This way it is also easier to debug your HTML as it is not only a PHP string.

这样,调试HTML也更容易,因为它不仅仅是一个PHP字符串。

#3


2  

You can set up and output buffer and then run the buffer through htmltidy. The tidy extension even has a specific function for the purpose. Just call this before you start outputting your html:

您可以设置并输出缓冲区,然后通过htmltidy运行缓冲区。整洁的扩展甚至具有特定的功能。只需在开始输出html之前调用它:

ob_start('ob_tidyhandler');

#4


1  

Although this solution does not override echo, you can get something close to echo with a newline. Add:

虽然此解决方案不会覆盖回声,但您可以使用换行符接近回声。加:

function e() {
    return o::singleton();
}

class o {
    private static $instance;

    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }

    public function __set($prop, $txt) {
        echo $txt . PHP_EOL;
    }
}

to your file, and then you can use:

到你的文件,然后你可以使用:

e()->o = "Line which ends in newline";

instead of echo.

而不是回声。

#5


0  

Another solution would be to separate your code from your layouts by using a proper templating engine.

另一种解决方案是使用适当的模板引擎将代码与布局分开。

#6


0  

You can indirectly overload echo() by using the __toString() magic method like so:

您可以使用__toString()魔术方法间接地重载echo(),如下所示:

<?php
class CleanOutput
{
    public $content;

    public function __construct($c) {
        $this->content= $c;
    }

    public function __toString() {
        return $this->content . '\r\n';
    }
}

$text= new CleanOutput('Hello world!');
echo $text;
?>

The above would output "Hello world!" with a newline and carriage return appended at the end. There's ways to further encapsulate this, but they are outside the scope of my answer.

以上将输出“Hello world!”在最后添加换行符和回车符。有进一步封装的方法,但它们超出了我的答案范围。

Edit:
As was noted, the above solution is slow/clumsy. Here's a more elegant solution using output buffering:

编辑:如前所述,上述解决方案缓慢/笨拙。这是使用输出缓冲的更优雅的解决方案:

<?
function clean_up($foo) {
   return $foo . '\r\n';
}
ob_start('clean_up');
ob_implicit_flush(true);
echo "Hello world!";
?>

This is faster and cleaner (although it technically doesn't 'override' echo).

这更快更干净(虽然它在技术上不会“覆盖”回声)。

#1


10  

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.

echo不是一个函数,而是一个语言语句。它无法重新定义。如果你想要美化你的输出标记,请看看Tidy。


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.

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

You can output several values with echo like this:

您可以使用echo输出多个值:

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

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

所以不需要在每一行上写回声。

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

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

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.

另外,获得漂亮标记的收益很少。如果您使用Firebug之类的工具来检查HTML,那么无论标记确实存在多么混乱,您都将获得格式正确的标记。此外,在拥有大量访问者的网站上,您经常会发现标记缩小,这与您尝试执行的操作相反,只是因为所有这些换行符和标签都会增加页面的重量,从而导致速度变慢页面加载和增加的流量成本。

#2


3  

You have various possibilities to output HTML.

您有各种输出HTML的可能性。

You can use the heredoc syntax:

您可以使用heredoc语法:

$html = <<<EOF
<h1>Rar<h1>
<span>Rar</span>
<p>Rar</p>
EOF
echo $hml;

Or (what is way better in my opinion), separate HTML from PHP. E.g. put all the PHP logic in the top of the file and the HTML after the PHP block:

或者(在我看来哪种方式更好),将PHP与PHP分开。例如。将所有PHP逻辑放在文件顶部,并在PHP块之后放置HTML:

<?php
   // all your PHP goes here
   $foo = 'bar'
?>
<!-- HTML comes here -->
<html>
  <body>
    <div>Hello <?php echo $foo; ?> </div>
  </body>
</html>

Variables can be printed as shown above. But these variables don't contain HTML.

可以如上所示打印变量。但是这些变量不包含HTML。

When you have to output HTML based on a condition, you can use the alternative syntax for control statements:

当您必须根据条件输出HTML时,您可以使用控制语句的替代语法:

<?php if($some_condition): ?>
    <h1>Rar<h1>
    <span>Rar</span>
    <p>Rar</p>
<?php endif ?>

This way it is also easier to debug your HTML as it is not only a PHP string.

这样,调试HTML也更容易,因为它不仅仅是一个PHP字符串。

#3


2  

You can set up and output buffer and then run the buffer through htmltidy. The tidy extension even has a specific function for the purpose. Just call this before you start outputting your html:

您可以设置并输出缓冲区,然后通过htmltidy运行缓冲区。整洁的扩展甚至具有特定的功能。只需在开始输出html之前调用它:

ob_start('ob_tidyhandler');

#4


1  

Although this solution does not override echo, you can get something close to echo with a newline. Add:

虽然此解决方案不会覆盖回声,但您可以使用换行符接近回声。加:

function e() {
    return o::singleton();
}

class o {
    private static $instance;

    public static function singleton()
    {
        if (!isset(self::$instance)) {
            $className = __CLASS__;
            self::$instance = new $className;
        }
        return self::$instance;
    }

    public function __set($prop, $txt) {
        echo $txt . PHP_EOL;
    }
}

to your file, and then you can use:

到你的文件,然后你可以使用:

e()->o = "Line which ends in newline";

instead of echo.

而不是回声。

#5


0  

Another solution would be to separate your code from your layouts by using a proper templating engine.

另一种解决方案是使用适当的模板引擎将代码与布局分开。

#6


0  

You can indirectly overload echo() by using the __toString() magic method like so:

您可以使用__toString()魔术方法间接地重载echo(),如下所示:

<?php
class CleanOutput
{
    public $content;

    public function __construct($c) {
        $this->content= $c;
    }

    public function __toString() {
        return $this->content . '\r\n';
    }
}

$text= new CleanOutput('Hello world!');
echo $text;
?>

The above would output "Hello world!" with a newline and carriage return appended at the end. There's ways to further encapsulate this, but they are outside the scope of my answer.

以上将输出“Hello world!”在最后添加换行符和回车符。有进一步封装的方法,但它们超出了我的答案范围。

Edit:
As was noted, the above solution is slow/clumsy. Here's a more elegant solution using output buffering:

编辑:如前所述,上述解决方案缓慢/笨拙。这是使用输出缓冲的更优雅的解决方案:

<?
function clean_up($foo) {
   return $foo . '\r\n';
}
ob_start('clean_up');
ob_implicit_flush(true);
echo "Hello world!";
?>

This is faster and cleaner (although it technically doesn't 'override' echo).

这更快更干净(虽然它在技术上不会“覆盖”回声)。