1,105,229 Community Members

End of Line/Newline Problem

Member Avatar
Woobag
Light Poster
33 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

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

Member Avatar
Woobag
Light Poster
33 posts since Jun 2006
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

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

Member Avatar
rudevils
Junior Poster in Training
80 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 10 [?]
Skill Endorsements: 3 [?]
 
0
 

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

Member Avatar
Walkere
Junior Poster in Training
57 posts since Jan 2008
Reputation Points: 19 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
laschofield
Newbie Poster
1 post since Apr 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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?

Member Avatar
ShawnCplus
Code Monkey
1,564 posts since Apr 2005
Reputation Points: 456 [?]
Q&As Helped to Solve: 269 [?]
Skill Endorsements: 39 [?]
Team Colleague
 
0
 

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

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

Member Avatar
sai pradeep
Newbie Poster
1 post since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
greg_s
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

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

Member Avatar
pflantzdog27
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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
Gilles-
Newbie Poster
1 post since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
oxihixo
Newbie Poster
1 post since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Your solution worked for me. Thank you very much.

northlandiguana
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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!

Member Avatar
EvolutionFallen
Posting Pro
504 posts since Aug 2009
Reputation Points: 107 [?]
Q&As Helped to Solve: 108 [?]
Skill Endorsements: 8 [?]
 
0
 

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'; ?>

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article