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

Link to comment
Share on other sites

\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  />.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.