A PHP "Echo-Newline" Function

Here's a nifty little function I came up for use within Onpub for outputting lines of HTML from PHP code. I call it the Echo-Newline function:


function en($str, $n = 1)
  echo $str;

  while ($n > 0) {
    echo "\n";


Here's an example of how this works:

en('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">');
en('<meta http-equiv="Content-Style-Type" content="text/css">');

This results in the printing of the string:

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">

To achieve the same thing with just a standard PHP echo call – with the same quoting style – you would have to write something like:

echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">' . "\n";
echo '<meta http-equiv="Content-Style-Type" content="text/css">' . "\n";

Notice that in order to make PHP output a newline, the newline escape sequence needs to be enclosed within double-quotes.

Instead of having a bunch of "\n" characters sprinkled throughout my code, I simply pass my single-quoted strings to the en() function and it handles the newline output for me cleanly and easily.

It also handles cases where I want to output 0 or more newlines after printing a string (it outputs 1 newline by default, that's what the $n argument specifies).

Anyway, very simple, but it results in cleaner-looking code. Feel free to use this in your own code if you find it useful.