472,107 Members | 1,256 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,107 software developers and data experts.

newline \n and tab \t Not Working in PHP 5.0.3 | Apache 2.0.52 | Windows XP Pro

The following examples (see PHP 5 for Dummies, p. 82)
do not produce the expected results:

$string = "Hello \nWorld";
echo $string;

and

$string = "Line 1 \n\tLine 2";
echo $string;

The first instance is for a line break such that the words are stacked:

Hello
World

The second instance is for a line break with a tabbed second line:

Line 1
Line 2

Do I need to change or add something in one of the configuration files?
Otherwise, is there a known fix for this? Thank you for any assistance.

Jul 17 '05 #1
7 15183
ga*****@cwnet.com wrote:
The following examples (see PHP 5 for Dummies, p. 82)
do not produce the expected results:

$string = "Hello \nWorld";
echo $string;

and

$string = "Line 1 \n\tLine 2";
echo $string;

The first instance is for a line break such that the words are
stacked:

Hello
World

The second instance is for a line break with a tabbed second line:

Line 1
Line 2

Do I need to change or add something in one of the configuration
files?
Otherwise, is there a known fix for this? Thank you for any
assistance.


Newlines and tabs are ignored by browsers when rendering HTML. If you
view the source of the page in your browser you will see the tabs and
newline breaks are there.

To do a line break in HTML you need to use the <br /> tag, or enclose
your sentance in <p> and </p> tags.

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #2
I noticed that Message-ID:
<11*********************@l41g2000cwc.googlegroups. com> from
ga*****@cwnet.com contained the following:
The following examples (see PHP 5 for Dummies, p. 82)
do not produce the expected results


I'd be interested to see what the book actually says the code does...

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #3
The book says the code should produce the two results shown in my
original post (which are copied there from the book). These expected
results appear below the examples in my post, a little farther down in
its text, as "first instance" and "second instance"--i.e., the first
result is to provide a line break (newline \n), and the second result
is to provide both a line break and a tab (newline tab n\t\).

Jul 17 '05 #4
ga*****@cwnet.com wrote:
The book says the code should produce the two results shown in my
original post (which are copied there from the book). These expected
results appear below the examples in my post, a little farther down in
its text, as "first instance" and "second instance"--i.e., the first
result is to provide a line break (newline \n), and the second result
is to provide both a line break and a tab (newline tab n\t\).


It should provide the expected results when you use View Source to see
the raw HTML code. However, when the browser displays the page it will
ignore the extra whitespace (spaces, tabs, and newlines are all whitespace).
Jul 17 '05 #5
Thank you.

It seems that the usefulness of the \n and \t features is limited to
formatting source code for easy readability.

It looks like <p></p> tags would be the best choice for onscreen
formatting. I imagine using CSS classes in them would permit such
things as the equivalent of tabbing, in addition to the default line
breaks.

Jul 17 '05 #6
ga*****@cwnet.com wrote:
The following examples (see PHP 5 for Dummies, p. 82)
do not produce the expected results:

$string = "Hello \nWorld";
echo $string;

and

$string = "Line 1 \n\tLine 2";
echo $string;

The first instance is for a line break such that the words are stacked:

Hello
World

The second instance is for a line break with a tabbed second line:

Line 1
Line 2

Do I need to change or add something in one of the configuration files?
Otherwise, is there a known fix for this? Thank you for any assistance.


Since browsers collapse all consecutive whitespace to a single space by
default, you could do this:

echo '<pre>';
echo $string;
echo '</pre>';

HTH,
JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #7
*** ga*****@cwnet.com escribió/wrote (13 Feb 2005 19:35:33 -0800):
Do I need to change or add something in one of the configuration files?


If you are expecting plain text output, add this to the very beginning of
the file:

header('Content-Type: text/plain');
--
-+ Álvaro G. Vicario - Burgos, Spain
+- http://www.demogracia.com (la web de humor barnizada para la intemperie)
++ Manda tus dudas al grupo, no a mi buzón
-+ Send your questions to the group, not to my mailbox
--
Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Nuno Paquete | last post: by
4 posts views Thread by arorap | last post: by
1 post views Thread by Gowri | last post: by
4 posts views Thread by adnanjunk | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.