Assuming that PHP_EOL directive and the new line "\n" do the same thing, neither are working. I've recently updated to PHP5 after a long hiatus from PHP altogether.

I have tried putting the newline character in single and double quotes, but to no avail...

Anyone have any suggestions?

Thanks in advance.
-Woobag

edit - forgot to say that the characters throw no errors or exceptions, but simply do nothing, as if they were not there

Recommended Answers

All 14 Replies

Well the \n isn't parsed for single quotes but it is for double quotes. Also, I'm not sure where you're trying to use it but if you are just outputting to an HTML page then it wont display the newline because whitespace characters(unless preempted with a <pre>) tag are ignored. You can use the nltobr(string) function to convert them however

I'm trying something like this...

<?php
$array = array('foo' => 'bar', 'baz', 'bat' => 2);

function printArray($array) {
	reset($array);
	while(key($array) !== null) {
		echo key($array) .": " . current($array) . "\n";
		next($array);
	}
}

printArray($array);

echo "This is a" . PHP_EOL . " new line.";

?>

Both are not working to produce new lines.

Sorry i didn't post this first.

edit - I tried wrapping the PHP code in <pre> tags and that did the trick, still trying to understand why it does not work without them.

Because in HTML whitespace characters (newline, space, tab) are ignored with rendering the page.

hi woobag,
i think u can use "<br>" instead of "\n"

edit - I tried wrapping the PHP code in <pre> tags and that did the trick, still trying to understand why it does not work without them.

As other people have pointed out, the reason this doesn't work without a <pre> tag is that HTML ignores normal whitespace characters.

The reason it does work with a <pre> tag is that that is what <pre> tags are designed for. The text inside of a <pre> tag will be displayed with all white space intact - in your case with a newline character.

As was suggested before, use a <br /> tag instead of an '\n' if you want to create a line break in your HTML output.

However, the '\n' still has a function and it is still doing something. If you view the HTML source for the output of your script, you should notice that the newline is observed.

So, if you're echo-ing large amounts of HTML, it can be a very good idea to include newline characters in there. That way your source code is still readable and not just one long jumbled line.

- Walkere

I'm having a different problem with the newline character and can find no references to it at all. I want to use the newline character for debugging purposes: to break up the source code into readable sections. I'm echoing <br> with no problems to create hard returns in my html. The problem is that when I follow my <br>s with /n's it actually prints the characters '/n' in my html!! So I'll have a hard return and then the characters /n print on the screen. I've used double quotes to parse the newline character. It still shows up on the screen. I've escaped it as //n and that doesn't work. I've used /r/n and that doesn't either. I'm still getting output and no errors, I just want /n to behave as a line break when I view the source code rather than have it appear on the screen!!

Any suggestions as to what I'm doing wrong?

A) Don't hijack year-old threads, create your own.

B) The escape sequence is a backslash, not a forward slash (\n, NOT /n)

<?php
include 'dbconnect.php';
$name = '';
$age = '';
$field = '';
$idnum = '';
$shiftt ='';
$batch = '';
$name=$_POST[

Use "\r\n" in double quotes. It works
Greg

commented: Thanks for your solution. It was very helpful. +0

thatnks. I was having the same issues - wondering why I couldn't create a new line with \n.
I wrapped the code in <pre> tags and that did the trick. Wondering which/if you include some type of reset for <pre> within you css file? Or is this included within popular resets like Eric Meyers?

Member Avatar for Gilles-

"\n\r" works (curiously not if single quotes why ?) BUT only if used with PC/Windows web client.

Use "\r\n" in double quotes. It works
Greg

Your solution worked for me. Thank you very much.

I'm having a similar problem, where it won't break the lines in the output e-mail and doesn't print the newline characters. But when I tried inserting <pre> and <br> tags, it just printed those as text in the e-mail, and didn't do anything. Help!

OK I'll try to clarify, since this thread keeps getting dug up from the grave:
<pre> isn't a one-off solution for getting line-breaks in your code.

"The pre element represents a block of preformatted text, in which structure is represented by typographic conventions rather than by elements" (http://www.w3.org/TR/html-markup/pre.html)

If you're resorting to this for your line breaks, it's a lack of understanding of line breaks in different languages.

Here are some options for line breaks in HTML and PHP:

  • HTML - <br>
  • XHTML - <br />
  • PHP -

    • "\n" for outputting in UNIX/Linux (Note DOUBLE quotes) for CLI/file output, etc.
    • "\r\n" for outputting in Windows (Note DOUBLE quotes) for CLI/file output, etc.
    • Note: If you are outputting to an HTML/XHTML document from PHP, see above.
    • The PHP constant PHP_EOL should use the correct End Of Line character for a given platform. So it should do the trick for you if you use it correctly. Example:

    <?php echo 'This line break ' . PHP_EOL . 'should work for non-HTML output'; ?>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.