Page 1 of 1
Can't output a new line "\n"
Posted: Mon May 07, 2007 1:00 pm
by Blue-Ray
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Hello,
It's a beginner question - sorry...
For some reason I can't get my script to output in several lines. All text is printed/echoed in one continous line. Even if I write: [b]echo "\n[/b]"; or [b]print "\n";[/b] the output does not move to a new line. (btw- other escape characters, like [b]\t[/b], don't work as well.
I also tried this script from the php manual:
Code: Select all
<?php
// Create a simple array.
$array = array(1, 2, 3, 4, 5);
print_r($array);
?>
And also here, the print_r rendered the array content in one line, and not in different rows, as shown in the official php manual.
I tried with IE and Firefox -- same behaviour.
Anyone has an idea ?
Thanks!
Blue-Ray
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Mon May 07, 2007 1:28 pm
by guitarlvr
for the issue with the /n, if you look at the source code for the page, it will be on a new line. /n is an ASCII charcater for new line, it is not an html tag. for the page to start a new line you will need to use <br />.
The print_r() fucntion printing on one line, try using <pre> and </pre> before and after the print_r().
Wayne
Posted: Mon May 07, 2007 1:29 pm
by RobertGonzalez
What server OS are you running? Also, when using print_r, try outputting it inside of HTML <pre></pre> tags, like so...
Code: Select all
<?php
$array = get_defined_functions();
echo '<pre>'; print_r($array); echo '</pre>';
?>
Posted: Mon May 07, 2007 1:44 pm
by Blue-Ray
Dear Everah,
I'm using Apache on Windows.
I tried within HTML and it worked. (btw - what exactly '<pre>' does? )
Also, how to make the '\n' to work?
Thanks
Posted: Mon May 07, 2007 2:00 pm
by andym01480
a PHP echo statement creates o/p to your browser. If you right click in IE and view source you will discover what \n has done created a new line in the code that got o/p to your browser, it doesn't get interpreted by the browser to show a new line on the screen. Thats what <br> is for
So
Code: Select all
<?php
echo "new line \r\n";
echo "newline \nnewline";
?>
Looks like
new line newline newline
in the browser
but
newline
newline
newline
when you view source in the browser
Code: Select all
<?php
echo "new line \r\n<br>";
echo "newline<br> \nnewline";
?>
Would output with new lines in the browser window and the view source would be pretty too
newline<br>
newline<br>
newline
Posted: Mon May 07, 2007 2:31 pm
by Blue-Ray
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
andym01480-
Thanks you very much for the detailed reply. Indeed it worked !
But an innocent question arise: if the <br> does it, why do we need the \n in the first place?
For example, I omitted the "\n" from your script script:
Code: Select all
<?php
echo "new line <br>";
echo "newline <br> newline<br>";
?>
and received the same output...
Thanks
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Mon May 07, 2007 2:33 pm
by guitarlvr
\n will force a new line when writing data to a text file or the like.
Wayne
Posted: Mon May 07, 2007 5:10 pm
by RobertGonzalez
\n (and sometimes \r) is charset representation of a new line. \t is a tab character. Since all text displayed in HTML is formatted according to HTML tags, those characters do nothing to the output of the text as HTML. To break a line, use the <br /> HTML tag.
<pre> is the HTML tag for predefined text, which does absolutely no formatting to the text between the tags. It outputs is exactly as it to the browser.
For a brief explanation of what I am talking about in the first part of this post, run this code and view source on the output:
Code: Select all
<?php
echo 'This is a line that is broken in HTML<br />';
echo '<br />This line will appear to be two lines below the line above.<br />';
echo 'This line should be just below the line above';
echo '<br /><br /><strong>This line is two lines below the line above and is bolded.</strong>';
?>
In the browser you will see:
This is a line that is broken in HTML
This line will appear to be two lines below the line above.
This line should be just below the line above
This line is two lines below the line above and is bolded.
But if you do a view source of the HTML page, you will see this:
Code: Select all
This is a line that is broken in HTML<br /><br />This line will appear to be two lines below the line above.<br />This line should be just below the line above<br /><br /><strong>This line is two lines below the line above and is bolded.</strong>
Now if you added the new line char in the output, when viewing the source of this code:
Code: Select all
<?php
echo 'This is a line that is broken in HTML<br />' . "\n";
echo '<br />This line will appear to be two lines below the line above.<br />' . "\n";
echo 'This line should be just below the line above' . "\n";
echo '<br /><br /><strong>This line is two lines below the line above and is bolded.</strong>';
?>
You would see this when viewing the source:
But if you do a view source of the HTML page, you will see this:
Code: Select all
This is a line that is broken in HTML<br />
<br />This line will appear to be two lines below the line above.<br />
This line should be just below the line above
<br /><br /><strong>This line is two lines below the line above and is bolded.</strong>
Understand a bit better now?
All is clear now - case closed.
Posted: Mon May 07, 2007 5:28 pm
by Blue-Ray
Thank you very much!! no wonder you won the Friendly PHP award.

Posted: Mon May 07, 2007 5:41 pm
by RobertGonzalez
Remember that in this years voting
