Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get newlines to work

Status
Not open for further replies.

Mike Lewis

Programmer
Jan 10, 2003
17,516
Scotland
I'm a novice in PHP, so please bear with me if I ask a very basic question.

I simply want to print a string with a newline in the middle. This is what I am doing:

Code:
<?php
 echo "This is the first line \n And this is the second";
?>

According to everything I've read, that should produce the desired output. But what I see in the output is this:

Code:
This is the first line And this is the second

The \n is eaten, but not processed. There is exactly one space between "line" and "And". I've checked that it's a backlash, not a forward slash, and the string is delimited with double-quotes.

Can someone please explain to me what I am doing wrong.

By the way, this is PHP 5.2.5, running under Linux.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
That depends on where you are outputting this.

If your looking at this in an HTML webpage for example. line breaks are ignored, and you need to supply the <br> html tag to get the line break.

If you output this to console or even a textarea in a webpage then the line break character should be \r\n

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Vacunita,

Thanks for your fast reply.

So far, I've only tried output to an HTML page. This is what I've got:

Code:
<span class="Quote">
 <strong>QUOTE OF THE DAY</strong><br /> 
 <?php echo GetQuote() ; ?></span>

And within the GetQuote() function:

Code:
return '<em>'.$quote.'</em>\n'.$source;

So, you're saying that, in those circumstances, the \n is ignored? If so, that's not a problem -- I've found it works OK with <br /> instead. It just highlights how much I've still got to learn.

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
If you were creating a file to disk and wanted to write text with LF, you would use \n or \n\r ... As you already figured out, if you are just pushing HTML document, <br /> is the HTML tag for a line break.

Notice that <br /> does not leave/add a blank line to separate paragraphs ... for this, use <p />



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Thanks to both of you for your help with this. I understand the issue more clearly now. In fact, in retrospect, it should have been more obvious. I know that HTML tends to ingore white space, and it seems reasonable that should happen regardelss of whether the white space is echoed from PHP or present inside the HTML.

SouthBeach: I take your point about <br /> and <p>. I'm comfortable with these HTML tags.

One small follow-up, if I may: In general, is echo a good way to send output from a PHP function to the underlying web page (as per my code, above)? The reason I ask is that I have also seen references to print and printf statements, and was wondering if either of these would be the preferred method, or whether it makes any difference.

Thanks again for your patience.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hi

Have you read What is the difference between echo and print?, recommended by the PHP documentation ?

The difference between [tt]echo[/tt] and [tt]print[/tt] is not so significant. I prefer [tt]echo[/tt] because it accepts multiple parameters, so I not have to concatenate everything into a single string. [tt]printf()[/tt] is very handy for formatting and outputting. But of course, the formatting part slows it down, so it should be used only when both formatting and outputting is necessary.

If no proper PHP code is involved, then could be better to just wrap the PHP code block around the text to output :
Code:
<?php
for ($i=0;$i<3;$i++) {
?>
Hello World !
<?php
}
?>

Feherke.
 
Sorry it took me so long to get back to this.

Yes echo is perfectly fine to be used.

In fact I also use echo over print or printf. As i came to PHP from a C++ background I always used printf to output things, however in PHP I find echo to be more versatile.

I specially like the heredoc syntax when I need to output large chunks of html that have very little or no PHP involved.

Code:
[red]echo<<<EOO[/red]
[green]
This can contain lots of HTML and even PHP variables if you need to.

No breaks are necessary and all $variables are evaluated as if surrounded by double quotes.

This also allows you to output things that may require double and single quotes interspersed without having to escape them. Which can get complex.
[/green]
[red]EOO;[/red]










----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
I find that including \n at the end of a string is still useful for diagnostics
it makes the output more readable if you do a View Source from your web browser.
 
Thanks for that, IPGuru.

I must say that I like the free-flowing nature of PHP. My main development tool (Visual FoxPro) has superb string-handling functions, but it's not ideal for coding very long strings as literals. You either have to scroll way over to the right of the screen, or break the string into several concatenated substrings. That seems less of a problem with PHP. (Although I still haven't got used to the dot as the string concatenation operator.)

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top