Jump to content


Photo

When do you use \n and what is it


  • Please log in to reply
5 replies to this topic

#1 psquillace

psquillace
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 16 June 2006 - 03:10 PM

Hello All:
I have been teaching myself php now for a little over 3 months and am stuck on arrays. I came across the \n variable and what it does for me.

I know that is represents a line break but how is that different than just <br />

whats more when I am writing an array all I do is use a ,comma and hit return to go to the next line.

Can someone clear up my head on all this as I am going bonkers. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

thanks

Paul

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 16 June 2006 - 03:23 PM

[!--quoteo(post=384588:date=Jun 16 2006, 03:10 PM:name=Paul Squillace)--][div class=\'quotetop\']QUOTE(Paul Squillace @ Jun 16 2006, 03:10 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Hello All:
I have been teaching myself php now for a little over 3 months and am stuck on arrays. I came across the \n variable and what it does for me.

I know that is represents a line break but how is that different than just <br />

whats more when I am writing an array all I do is use a ,comma and hit return to go to the next line.

Can someone clear up my head on all this as I am going bonkers. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

thanks

Paul
[/quote]

\n line brake is used to get a new line example.

echo "My name is:\n john";

result
my name is
john


The same as <br> but brake has no ecaping\

echo " my name is:<br>john";

result
my name is:
john


heres my code that i learned arrays


example 1
<?

$lucky=array("food","dog","home","foot","pool","leg","body","head","teth","ear");  
for($i=0; $i<count($lucky); $i++){
echo "<br>$lucky[$i]<br>normal array";
}

?>


<?
example 2

$lucky=array("food"=>"dog","home"=>"foot","pool"=>"leg","body"=>"head","teth"=>"ear");  

while(list($i, $j,)=each($lucky)){
echo "<br>".$i."<br>".$j."<br>assotate array";
}

?>




<?
example 3

$lucky=array("dog","home","foot","pool","leg","body","head","teth","ear");  

foreach( $lucky as $i => $j){

echo "<br>".$i."<br>".$j."<br>forech array";

}
?>


<?
example 4

$lucky=array(array("food","dog","home","foot","pool","leg","body","head","teth","ear"),
             array("food","dog","home","foot","pool","leg","body","head","teth","ear"));  
echo"<br><br> ".$lucky[1][0]."<br>double array";
?>

<?
example 5

$lucky=array("myarray1"=>array("food","dog","home","foot","pool","leg","body","head","teth
","ear"),
"myarray2"=>array("food"=>"a","home"=>"b","pool"=>"c","leg"=>"d","teth"=>"e")
);  
echo"<br><br>".$lucky[myarray1][1]."<br>double array named";
?>

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 16 June 2006 - 03:42 PM

The "\n" is the line break character on Linux/Unix machines. On Windows it is "\r\n", and on the Mac it is "\r". It's primarily used as line termination characters in flat files. It is also used by many people to format generated code so that it is readable by humans. It is rarely used to format output to the browser, since all browsers ignore them except if the string being sent is surrounded by the '<pre></pre>' tags.

Example:
<?php
$str_with_cr = "This string\nhas many\n newline\n characters in it";
echo $str; // this will show on the screen as "This string has many newline characters in it"
echo '<br>'; 
echo nl2br($str); // the nl2br() function inserts the "<br />" tag before each newline character so the output looks like you intended
echo '<br><pre>' . $str .'<pre>'; // This causes the browser to print the string as is, with applying it's own styling, so it looks correct
?>

Try this code, it should help you understand the newline character.

Ken

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 June 2006 - 04:43 PM

mostly i only see it used when generating an email message to be sent to an email address.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 psquillace

psquillace
  • Members
  • PipPipPip
  • Advanced Member
  • 94 posts

Posted 16 June 2006 - 06:30 PM

Thanks all for the repsponse to my question as this was driving me crazy all night.

I understood it was used for line breaks but not the difference between \n and <br />

thanks again for all your help, you guys are the best [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]

#6 dptr1988

dptr1988
  • Members
  • PipPipPip
  • Advanced Member
  • 372 posts

Posted 16 June 2006 - 06:41 PM

In my understanding, the \n escape sequence is converted to a number (0Ah) during the execution of your script. Plain text editors will normally go to a new line when they read that number.

The < BR/> is a tag the tells the browser to make a newline because HTML browsers do not make new lines when the read the number 0Ah.

So '\n' represents a newline for text editors, < BR/> represents a new line for HTML browsers.




Need more help with your project? One of the thousands of programmers, web designers or artists at <a href="http://www.rentacode...d_6764522">Rent A Coder</a> would be happy to help.

Disclaimer: Free advice is usually worth what you paid for it. ( or at least when it's coming from me! )




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users