PDA

View Full Version : PHP 5: \n does not generate newline



boococp
February 13th, 2010, 08:17 PM
Running PHP5 on Ubuntu 9.10

The following code:

<?php
$v="Test line";
$i=1;
while ($i <=10) {
printf ("%s\n", $v);
$i++;
}
?>

generates the following output in Firefox:

Test line Test line Test line Test line Test line Test line Test line Test line Test line Test line

Can someone tell me why \n is not generating newlines?

MadCow108
February 13th, 2010, 08:20 PM
if your using php for a webpage: newlines on websites (html) are written as <br/>
else: ignore this post

Reiger
February 13th, 2010, 09:16 PM
In order to get line breaks, use the PHP_EOL constant. This is the "platform independent" end-of-line character/string.

miklcct
February 14th, 2010, 01:00 PM
Have you set your Content-Type correctly (it should be text/plain)?

Hellkeepa
February 16th, 2010, 09:44 PM
HELLo!

Unless a web browser is told otherwise, as noted above by miklcct, it'll assume that it is a web page. Thus it ignores the newlines, and treats them as a single space instead (as per the standard).

Happy codin'!