CyberShot Posted October 17, 2009 Share Posted October 17, 2009 I was looking at some of the things you could do with php and one of the things I have tried is \n. used this way while(list($key, $val) = each($myArray)){ echo "$key => $val\n"; I expect it to echo the values out on a newline but never does any time I use \n I have tried \r. What am I missing Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/ Share on other sites More sharing options...
Gayner Posted October 17, 2009 Share Posted October 17, 2009 use break tag if \n doesnt work lol Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938902 Share on other sites More sharing options...
MatthewJ Posted October 17, 2009 Share Posted October 17, 2009 while(list($key, $val) = each($myArray)){ echo $key." => ".$val."\n"; The \n has to be inside quotes... but yes, you could use while(list($key, $val) = each($myArray)){ echo $key." => ".$val."<br />"; as well Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938903 Share on other sites More sharing options...
Mark Baker Posted October 17, 2009 Share Posted October 17, 2009 If you view the page source for the generated html, you'll find that it does insert the \n The issue is that browsers don't display \n, same as they reduce all consecutive white spaces to a single space character. Instead, HTML uses the <br /> tag You could use echo "$key => $val<br />";[code=php:0]or PHP also provides a function to help you with this:[code=php:0]echo nl2br("$key => $val\n"); Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938904 Share on other sites More sharing options...
Alex Posted October 17, 2009 Share Posted October 17, 2009 \n doesn't change the view that you see in the browser; the same way returns in html doesn't cause text to goto the next line. You'll see it on the next line if you goto view page source. Otherwise use <br />. Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938905 Share on other sites More sharing options...
CyberShot Posted October 17, 2009 Author Share Posted October 17, 2009 well my issue is that I am going through the php documentation and it gives code like this $fruit = array('a' => 'apple', 'b' => 'banana', 'c' => 'cranberry'); reset($fruit); while (list($key, $val) = each($fruit)) { echo "$key => $val\n"; } and then says that it will output a => apple b => banana c => cranberry but then it doesn't. So I am trying to figure out if most or all or just some of the info on the site is B.S Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938907 Share on other sites More sharing options...
CyberShot Posted October 17, 2009 Author Share Posted October 17, 2009 I see. Thanks guys. very helpful Quote Link to comment https://forums.phpfreaks.com/topic/178065-solved-question-about-n/#findComment-938909 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.