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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Very simple question - "echo" 1

Status
Not open for further replies.

Hondy

Technical User
Mar 3, 2003
864
GB
Hi -I'm just starting out in PHP, and want to use the echo command. I want to split the sentence into new lines. I've read /n is used for that. When I do this it has no effect, what am I doing wrong please?
Taken from
<?php
echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
?>

OUTPUT=
This spans multiple lines. The newlines will be output as well.

What am I doing wrong? PHP is working as I can retrieve and display IP address info etc but all my results end up on one line :/

Thanks
 
you can try:

<?php
echo "<pre>This spans\nmultiple lines. The newlines will be\noutput as well.<pre>";
?>

I would however do:

<?php
echo nl2br("This spans\nmultiple lines. The newlines will be\noutput as well.");
?>

as that would generate <br>

Olav Alexander Mjelde
Admin & Webmaster
 
If you're popping this into an HTML page, then a newline actually doesn't do anything to the output. If you view source the page you've included the php in, then you will see that it probably did, in fact, insert a new line. I would guess the View Source would look something like this:

Code:
This spans
multiple lines.  The newlines will be
output as well.

When you're echoing into an HTML document, then you can use standard HTML in the echo, like so:

Code:
<?php
  echo("This spans<BR>multiple lines.  The newlines will be<BR>output as well.");
?>

Marc
 
Thanks DaButcher and HMarcBower, both interesting solutions.

DaButcher, would you mind explaingin what nl2br does? And why you both used brackets?

HMarcBower - you are right about the view source, how annoying is that?!
 
Hondy, you can make newlines with \n but html renders newlines as single spaces. They are mostly used to make your code look better organized (putting newlines at the end of echoed html tags). You don't need to put brackets with echo, I think that was a copy&paste blunder with Marc. DaButcher used it because he was running nl2br function on that statement. nl2br is a simple function that replaces every occurence of \n with <br />, which is html's tag for line breaks.
 
Hondy:
\n -> NL (newline)
in html, newline is not used, but <br> is.
(br = break)

nl2br, simply makes \n into <br> (HTML).
When/Why do you need it?

If you have some sort of guestbook script, where the users can enter comments, they often can use *multiline*, eg. it will be posted as \n.

in HTML, you can not display \n, without <pre></pre>, so it's better to convert it to <br>

You could use:
$data = str_replace("\n", "<br>" $data);

but, why reinvent the wheel?

ps. I think I remember that nl2br makes <br /> (XHTML compliant breaks)

If you wish more info on the matter:

php.net is a great resource when you have learned how to use it.

Olav Alexander Mjelde
Admin & Webmaster
 
Vragabond -I can't see why you would use nl2br and /n when you could just use <br>?

Anyway thanks for clarifying, I'll get the hang of it soon I hope :)
 
Thanks DaButcher, you replied at the same time as my last reply :) - Have a star for your detailed explanation!
 
I always use brackets with echo... they aren't necessary, but it lends a nice consistency for my own purposes. :)

Marc
 
Hondy:
I have one example where you might want to use \n and \t
\t -> tab(ulator)

If you do this:

Code:
echo "Loop 1:<br />";
for ($i = 0; $i < 10; $i++) {
  echo "Loop_1 nr: " . $i . "<br />";
  }
echo "<hr />Loop 2:<br />\n";
for ($i = 0; $i < 10; $i++) {
  echo "Loop_2 nr: " . $i . "<br />\n";
  }

Then you can look at the source...

You will see that the first loop, puts all source in one line!

The second loop, will put newlines in the source-code!

ps. I stated something slightly wrong above:
nl2br() will not replace \n with <br /> or <br>, but it will append <br /> or <br> in front of \n, where \n is in the nl2br() function.

ps. when do you use \t?
if you echo something that is "child" of a "parent".

like:
Code:
<table width="50%" border="1">
\t<tr>
<?php
for ($i = 0; $i < 10; $i++;) {
  echo "\t\t<td>{$i}</td>\n";
}
?>
\t</tr>
</table>

Olav Alexander Mjelde
Admin & Webmaster
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top