psquillace Posted June 16, 2006 Share Posted June 16, 2006 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\" /] thanksPaul Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/ Share on other sites More sharing options...
redarrow Posted June 16, 2006 Share Posted June 16, 2006 [!--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) [snapback]384588[/snapback][/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\" /] thanksPaul[/quote]\n line brake is used to get a new line example.echo "My name is:\n john";resultmy name isjohnThe same as <br> but brake has no ecaping\echo " my name is:<br>john";resultmy name is:johnheres my code that i learned arrays[code]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";?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/#findComment-46298 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2006 Share Posted June 16, 2006 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:[code]<?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 intendedecho '<br><pre>' . $str .'<pre>'; // This causes the browser to print the string as is, with applying it's own styling, so it looks correct?>[/code]Try this code, it should help you understand the newline character.Ken Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/#findComment-46309 Share on other sites More sharing options...
.josh Posted June 16, 2006 Share Posted June 16, 2006 mostly i only see it used when generating an email message to be sent to an email address. Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/#findComment-46345 Share on other sites More sharing options...
psquillace Posted June 16, 2006 Author Share Posted June 16, 2006 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\" /] Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/#findComment-46398 Share on other sites More sharing options...
dptr1988 Posted June 16, 2006 Share Posted June 16, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/#findComment-46405 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.