Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

my php source:

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

function echo($string)
{
 echo $string . "\r\n";
}
like image 993
yretuta Avatar asked Feb 02 '10 08:02

yretuta


People also ask

Can you echo a function in PHP?

Definition and Usage. The echo() function outputs one or more strings. Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

What is the purpose of echo function in PHP?

PHP echo statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc. Some important points that you must know about the echo statement are: echo is a statement, which is used to display the output. echo can be used with or without parentheses: echo(), and echo.

What is the difference between return and echo in PHP?

Echo is for display, while return is used to store a value, which may or may not be used for display or other use.

How do you overwrite a class in PHP?

To override a method, you redefine that method in the child class with the same name, parameters, and return type. The method in the parent class is called overridden method, while the method in the child class is known as the overriding method.


2 Answers

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.

like image 111
Gordon Avatar answered Nov 08 '22 23:11

Gordon


You have various possibilities to output HTML.

You can use the heredoc syntax:

$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
   // 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.

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

<?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.

like image 33
Felix Kling Avatar answered Nov 08 '22 23:11

Felix Kling