Re: Newline character problem...

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In addition to what was mentioned below, you can also wrap your text in <PRE></PRE> tags to have it output exactly as you've formatted it:

echo "<PRE>\n";
echo "one\n";
echo "two\n";
echo "</PRE>\n";

Actually, I don't think either of these methods is going to output what you want.  Even though \n is newline, it's still interpreted as newline/carriage return.   Unix, Mac and DOS/Windows PC's all use something different for a newline character.  I believe Unix was \n, Mac was \r at some point and DOS/Windows PC's used \n\r.  Think my old Amiga used \r as well.  Goofy.

Anyway, it's proably because of this that you can't just do \n and expect it to be represented as just a newline (sans carriage return).

If you use the <PRE> tag, then you can manually do what you want to do:

echo "<PRE>\n";
echo "one\n";
echo "   two";
echo "</PRE>\n";


If you don't use the <PRE> tag, there's an HTML entity that you can use for a hard space..  &nbsp;   Unfortunately in HTML, if you stack a bunch of spaces, it compresses them to one space.

echo "one            two";
..and..
echo "one two";

Will both result in:
one two

If you use &nbsp; instead of spaces, it'll space it out further:

echo "one&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;two";

Ugly but it works.

Unfortunately (again), if you don't use the <PRE> tags, you'll have to remember to set a monospace font face or you won't get the text alignment you might want.


One other side nugget related to this (formatted and spaces and all that nonsense), you can use the <NOBR></NOBR> tag to tell HTML not to word-wrap the text within it.  Comes in handy when you want to force a column heading to be one line even though it's got a space in it and your browser wants to wrap the line.

Good luck! Hope this helped a little.

-TG

= = = Original message = = =

Robert napisal(a):
> I'm new to PHP but not programming in general. I have used C++ for a while
> and I'm familiar with the newline character as it's basically the same in
> PHP.
> This is the most basic of examples:
> print 'one' ;
> print "\n" ;
> print 'two' ;
> 
> The output of this when accessed on my server is: one two
> It should be: one
>                    two

Hi
well this isn't apache or php related, as it's webbrowser related, and 
even more, it's correct.

The thing is, what You see in Your browser is an interpreted version of 
the html outputed by the php (obvious?), so if You do a html file, and put:
one
two
in there, and view it with Your browser, it will still look like this:
one two
to get the expected (as in C/C++) result, You need to add a <br> or any 
other line breaking method in there (<p>one</p><p>two</p> is another one)
Now, if You'd see the generated source code, You'd notice that there is 
in fact the line break as it should be :)
so it is good to use '\n' to get the output code clear to read, for 
debugging and so..

Best wishes
?ukasz Hejnak


___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux