printing newlines

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jaymoore_299
Forum Contributor
Posts: 128
Joined: Wed May 11, 2005 6:40 pm
Contact:

printing newlines

Post by jaymoore_299 »

Code: Select all

for ($i = 0; $i <= 100; $i++) {
print $i . "\n";
}
why doesn't this print each in its own line on my browser? I get a continuous line of this 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

I even tried replacing the \n with \r\n and it still gives me one continuous line. How do I output each in its own line?
User avatar
ambivalent
Forum Contributor
Posts: 173
Joined: Thu Apr 14, 2005 8:58 pm
Location: Toronto, ON

Post by ambivalent »

Try

Code: Select all

print $i . "<br/>";
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

view the source of the page, it's printing newlines but spec says browsers ignore whitespace & newlines
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

\n and \r do work well with files.
its been said that more than one space is ignored by browsers so you could see some relation between \n and spaces. ofcourse i think, \n is a collection of white spaces which are ignored by the browser. <br /> helps to get the format we require.
<br /> does not put a new line in the view source but its interpreted by the browser.
for ex:
if you type something like this in html

Code: Select all

<body>
<p>there is a                           huge                   gap betwen words</p>
</body>
you can see that there is only one space between a and huge and huge and gap. it automatically discards those spaces you have to use &nbsp; to get the number of spaces you require.
Post Reply