Steven Posted May 31, 2008 Share Posted May 31, 2008 Hello,I am beginning PHP, and began reading through "Learning PHP 5" by O'Reilly. The book is good and for the most part easy to understand. But I'm having trouble with simple linebreaks! "\n" is placing spaces between words, not linebreaks. This is my code: <?php$hello = 'hello, sir!';$finish = 'did the break work?';print $hello;print "\n";print $finish;?> And this is what it outputs:hello, sir! did the break work? I don't understand why it isn't working. My host's PHP version is 4.4.7, if that makes any difference. Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 31, 2008 Share Posted May 31, 2008 This is because the operating systems vary regarding what a linebreak is. Unix uses \n, Windows uses \r\n, and Mac uses \r. The constant PHP_EOL (End Of Line) contains a platform-independent linebreak.EDIT: So that would be print $hello;print PHP_EOL;print $finish; EDIT2: Woops, Ingolme's right too. Link to comment Share on other sites More sharing options...
Ingolme Posted May 31, 2008 Share Posted May 31, 2008 If you're outputting this to the browser you're not going to see the line break because HTML doesn't show line breaks from the source code.Try this:<?phpheader("Content-Type: text/plain");$hello = 'hello, sir!';$finish = 'did the break work?';print $hello;print "\n";print $finish;?> Link to comment Share on other sites More sharing options...
Fukushousha Posted May 31, 2008 Share Posted May 31, 2008 I was wondering about that in php too. I thought that the trick would be to use the usual C language printf function. It wasn't .I heard(have not verified it) that \n works in preformatted text only.So instead since it was output to the browser I used the html code <br /> in my echo/printf functions. Might not be what is right to do but it worked for me. Mentioning it so you can know all your options Link to comment Share on other sites More sharing options...
Steven Posted May 31, 2008 Author Share Posted May 31, 2008 Thanks guys.Fuku- a friend told me to try <br /> as well, and it worked. But I don't know if that is "cosher" to use. I'm used to CSS/XHTML and having everything be 'standardized', do things like that matter in PHP? I mean, is code using for example <br /> rather than print PHP_EOL worse off?Thanks again! Link to comment Share on other sites More sharing options...
Jesdisciple Posted May 31, 2008 Share Posted May 31, 2008 PHP_EOL is only for preformatted text (or source-code). You can see its effect by viewing a page's source or using the text/plain media type. <br /> (or <br> in HTML) is for when PHP's output will be read by a browser as XHTML. Link to comment Share on other sites More sharing options...
Ingolme Posted May 31, 2008 Share Posted May 31, 2008 Unless your intention is to actually write an HTML page, don't use <br> (or <br />), because if a browser happens to interpret it as text/plain it will show "<br>" in the output.Try your code with a text/plain header:header("Content-Type: text/plain"); Link to comment Share on other sites More sharing options...
justsomeguy Posted June 3, 2008 Share Posted June 3, 2008 Don't confuse the output of PHP with what you see in the browser. If you want to output a line break, fine. Run this code: <?phpecho "Line 1\n-----------";echo "\n\n\n\n\n\n\n\n";echo "Line 2";?> When you pull it up in the browser, go to View->Source. That is the output of PHP. What you see in the browser is not the output of PHP, it is the browser's interpretation of the output of PHP. The browser does not interpret a newline as a line break for the rendered code. If you have this in HTML: <div>Thisis some text and an image<img src="image.jpg"></div> All of that, including the image, is going to be rendered on the same line even though there are line breaks in the source. That's how the browser renders it, so when you look at output in the browser then you're looking at the browser's rendering of the output, not the actual output. If you want to see the actual output, then send a content type header saying that the content is plain text instead of HTML, and then you'll see the linebreaks in the browser. Or else, to see the output of PHP you need to view the source of the rendered page. If you want to output a line break for the browser (an HTML line break), then that is a <br> tag. You can use this to set the content type and display the output as plain text:header('Content-type: text/plain'); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.