Current location: Hot Scripts Forums » Programming Languages » PHP » why isnt \n breaking lines


why isnt \n breaking lines

Reply
  #1 (permalink)  
Old 08-11-05, 03:05 AM
lppa2004 lppa2004 is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
why isnt \n breaking lines

This is kind of odd to me... i just been playing around with some code and in the following code it wont break lines when i use the \n... but it will do it with <BR>

Im confused why thsi doesnt work when i use it.. yet ive seen it work in previous scripts, and they use it througouth the PHP Manual/Documentation on the PHP website...

PHP Code:

<?php


$states 
= array(
           
'AL' => 'Alabama',
           
'AK' => 'Alaska',
           
'AZ' => 'Arizona',
           
'AR' => 'Arkansas',
           
'CA' => 'California',
           
'CO' => 'Colorado',
           
'CT' => 'Connecticut',
           
'DE' => 'Delaware',
           
'DC' => 'Dist Columbia',
           
'FL' => 'Florida',
           
'GA' => 'Georgia',
           
'HI' => 'Hawaii',
           
'ID' => 'Idaho',
           
'IL' => 'Illinois',
           
'IN' => 'Indiana',
           
'IA' => 'Iowa',
           
'KS' => 'Kansas',
           
'KY' => 'Kentucky',
           
'LA' => 'Louisiana',
           
'ME' => 'Maine',
           
'MD' => 'Maryland',
           
'MA' => 'Massachusetts',
           
'MI' => 'Michigan',
           
'MN' => 'Minnesota',
           
'MS' => 'Mississippi',
           
'MO' => 'Missouri',
           
'MT' => 'Montana',
           
'NE' => 'Nebraska',
           
'NV' => 'Nevada',
           
'NH' => 'New Hampshire',
           
'NJ' => 'New Jersey',
           
'NM' => 'New Mexico',
           
'NY' => 'New York',
           
'NC' => 'North Carolina',
           
'ND' => 'North Dakota',
           
'OH' => 'Ohio',
           
'OK' => 'Oklahoma',
           
'OR' => 'Oregon',
           
'PA' => 'Pennsylvania',
           
'RI' => 'Rhode Island',
           
'SC' => 'South Carolina',
           
'SD' => 'South Dakota',
           
'TN' => 'Tennessee',
           
'UT' => 'Utah',
           
'VT' => 'Vermont',
           
'VA' => 'Virginia',
           
'WA' => 'Washington',
           
'WV' => 'West Virginia',
           
'WI' => 'Wisconsin',
           
'WY' => 'Wyoming'
           
);

           foreach (
$states as $state) {
                   echo 
"The State is $state \n";
                   }
                   print 
$states['IN'];

?>
Reply With Quote
  #2 (permalink)  
Old 08-11-05, 03:07 AM
lppa2004 lppa2004 is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
i mean its no big deal i guess, i can use <BR>.. but i just done see why they would include it in the manual if it doesnt work
Reply With Quote
  #3 (permalink)  
Old 08-11-05, 03:23 AM
darkfreak's Avatar
darkfreak darkfreak is offline
Newbie Coder
 
Join Date: Jun 2004
Location: Kuopio, Finland, Europe
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
This is not a PHP issue, but it's related to HTML and to your browser. Use <pre> and </pre> -tags of HTML to define pre-formatted content - this way your browser will output the contents between those tags "properly".

If you output your $string with "\n" -newlines into a file and open it with some editor, you'll see that newlines work. Or, you can view the page source in your browser to see that \n does actually work

PHP Code:

           echo "<pre>";

           foreach (
$states as $state) {
                   echo 
"The State is $state \n"
                   } 
                   print 
$states['IN'];
           echo 
"</pre>"

Last edited by darkfreak; 08-11-05 at 03:29 AM.
Reply With Quote
  #4 (permalink)  
Old 08-11-05, 03:25 AM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
use
PHP Code:

print nl2br($states['IN']); 

the \n produces a line break but you cant see line breaks on an HTML page (unless you view the source) thus use a function such as nl2br which will add a <br> tag whenever there is one!
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
  #5 (permalink)  
Old 08-11-05, 03:45 AM
lppa2004 lppa2004 is offline
Newbie Coder
 
Join Date: Oct 2004
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
well im using "PHP Expert Editor" and then using the "RUN" command, i just assumed it would output it correctly... Okay well i used the <PRE> tags and those work perfectly, and will save me some typing in the future... thanks for the info guys... This was just one of those things i couldnt seem to grasp, prolly seemed liek a dumb question to most of you whose been programming awhile, but im newbie and want all the insight i can get.. :-)
Reply With Quote
  #6 (permalink)  
Old 08-11-05, 06:58 AM
dennispopel dennispopel is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 263
Thanks: 0
Thanked 0 Times in 0 Posts
Hello,

The manual examples will work correctly (I mean newlines) if run with the CLI version.
__________________
onPHP5.com - PHP5: Articles, News, Tutorials, Interviews, Software and more
Reply With Quote
  #7 (permalink)  
Old 08-11-05, 10:55 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
OMG, FiRe is using nl2br() function!!?
sorry for being off-topic..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #8 (permalink)  
Old 08-11-05, 12:39 PM
FiRe FiRe is offline
Code Guru
 
Join Date: Oct 2004
Location: UK
Posts: 801
Thanks: 0
Thanked 0 Times in 0 Posts
hee hee, i cba to put my lil str_replace on there
__________________
Alexa Share <-- Trade virtual shares in websites with this online game.

codR.us <-- Submit and vote for your favorite code snippets with codR.us.

XEWeb.net <-- The ultimate PHP resource network.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
extra lines in output TimHub General HotScripts Site Discussion 0 03-01-05 01:07 PM
First ten lines from file somewhat deviant PHP 4 11-01-04 06:22 PM
Count lines from a $_GET variable zcorpan PHP 5 10-18-04 12:30 AM
List lines with only 7 characters from a file Lap PHP 2 06-20-04 04:34 AM
Wht does these script lines mean? karthik_vina Script Requests 0 04-14-04 06:52 AM


All times are GMT -5. The time now is 07:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.