Jump to content

[SOLVED] question about \n


CyberShot

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/178065-solved-question-about-n/
Share on other sites

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");

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.