Trouble with /n

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
jonah
Forum Commoner
Posts: 31
Joined: Fri Jun 20, 2003 12:23 am

Trouble with /n

Post by jonah »

I see this subject has been hashed over but I cannot seem to
make the \n linefeed work in my version of PHP (4.3.1) running
on Apache 1.3.27 in Slackware Linux 9.0.

As an example:
<?
$fruits = array( 'strawberry' => 'red' , 'banana' => 'yellow' );
echo "A banana is $fruits[banana].\n";
echo "\n";
echo "A banana is $fruits[banana].\n";
?>

The above never produces a line feed. I have also tried "\n\n"; &
"\r\n"; with the same result. The only thing that will work is to replace
the \n with the html <br> tag. I am viewing the php output in IE 5.5.

I thought this might be the result of improper settings in php.ini but
a perusal of that file revealed nothing that would lead me to believe
there was anything to alter there.

While this is not a big problem, I am totally befuddled by the
inoperability of this piece of code and wish to track down the
problem.

Anyone have an idea? TIA
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

You must use <br/> if you want a new line on your HTML page. \n or \r\n are used only in text files, like you said for new line. So if you put a \n into your HTML code, the internet browser won't display it like a new line. It's nothing wrong with your Apache, PHP or Linux. Everything's ok. :D
Drachlen
Forum Contributor
Posts: 153
Joined: Fri Apr 25, 2003 1:16 am

Post by Drachlen »

must use <br/> ? I always use <br>, why the slash? the only time ive seen it used with a slash was like this: <br /> with a space.
lcidw
Forum Commoner
Posts: 58
Joined: Mon Apr 28, 2003 8:55 am
Location: Netherlands

Post by lcidw »

The <br> is an old standard. The W3C (World Wide Web Consortium) decided that every tag should have a closing tag, or be the closing tag at the same time. Just as <p></p> the new line <br> became <br />.

http://www.w3schools.com/tags/tag_br.asp
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

<br /> (there's nothing wrong wiht <br/>) it's a more XHTML style of typical HTML <br>. The slash means that this is one tag element, and every HTML element must be closed. So if you have paragraph <p> it's a block element so you close it in typical way </p>.

UPDATE: After posting I saw that we had the same ideas like Icidiw :D :roll:
User avatar
discobean
Forum Commoner
Posts: 49
Joined: Sun May 18, 2003 9:06 pm
Location: Sydney, Australia
Contact:

Post by discobean »

so this would mean that with any element that dosn't have a closing tag, like delorian pointed out, with his <p></p> example.. You would use <tagname />

a good example is the <meta> tag... which dosn't have a closing element, so you'd use <meta attributes="" />

ta da.

witam z australii!
User avatar
trollll
Forum Contributor
Posts: 181
Joined: Tue Jun 10, 2003 11:56 pm
Location: Round Rock, TX
Contact:

Post by trollll »

With the single exception of the "<!DOCTYPE>" tag. But doctype has its own special place...
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I don't think this applies to any elements that start with <!
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

still useful

Post by phpScott »

<br /> is the right way for new lines in HTML but using the \n in your code is also very helpful especially generating html as it will format it nicely if you ever have to do a view source to see what the html looks like you are generating.

ie

Code: Select all

<?php
print"something";
print "<br />\n";
print"something else";
?>
will give you nicely formated hmtl.

phpScott
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

It is better currently to use <br /> rather than <br/> because older browsers (like Netscape 4 :x ) barf if there isn't a space between the tag name and the closing slash.

Mac
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

I wouldn't bother trying to make Netscape 4 happy. If you do, you'll end up in a heap in the corner, never wanting to see daylight again... oh hang on, I think I just stumbled across the reason that geeks are like what they're like.. cross-browser support.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

[OT]
releasedj wrote:I wouldn't bother trying to make Netscape 4 happy.
I don't have any choice, personally I would settle for 'degrades gracefully' but the powers that be wish the site to look pretty much the same in Netscape 4 as it does in the latest browsers :cry: ... But slowly but surely I am winning the standards over backwards compatibility hacks argument :) . XHTML strict here I come...

Mac
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

Good luck. Luckily, I work for people who understand that if everyone coded with backwards compatibility in mind, then we would never move forward.
Post Reply