Can I override the PHP built-in function echo()?

后端 未结 6 1179
無奈伤痛
無奈伤痛 2020-12-11 15:02

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

my php source:

echo \'

Rar

\'; echo \'Rar\'; e

6条回答
  •  失恋的感觉
    2020-12-11 15:34

    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.


    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.

    You can output several values with echo like this:

    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.

    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.

提交回复
热议问题