Jump to content

Recommended Posts

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
Link to comment
https://forums.phpfreaks.com/topic/12159-when-do-you-use-n-and-what-is-it/
Share on other sites

[!--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\" /]

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
[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]
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 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
?>[/code]

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

Ken
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\" /]
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.



This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.