Problem with Escape Sequences, unclear on how to implement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
HungryMind
Forum Commoner
Posts: 41
Joined: Fri Jun 20, 2003 10:33 am
Location: Alameda, CA

Problem with Escape Sequences, unclear on how to implement

Post by HungryMind »

Hello everyone. I am new to PHP, but I have been lurking for a short period of time here. I will probably come back to this place and have many many questions for you guys ;). As for the following question, I have already searched a few places for it, but it seems like I am the only one who doesn't understand what is going on.

Code: Select all

print $myname . " is " . $myage . " years old.\n";
print "hello."
My problem is with the "\n" escape sequence. When I try to type the above line, it doesn't display as I want it to. Why won't the "\n" do its job and break to a new line? As of now, I am seeing this:

Code: Select all

(string variable) is (numeric variable) years old. hello.
My desired result is, of course, the following.

Code: Select all

(string variable) is (numeric variable) years old.
hello.
Okay, time for the questions: Why isn't "hello" breaking to a new line? Did I type something incorrectly? I tried doing the same thing with a different escape sequence, such as "\$" and "\\" and these display correctly when placed in the exact same position. Is there a fix or am I going to have to result to "<br>" tags?

One more clarifying question: What is the difference between a new line break (\n) and a carriage return (\r)? I cannot seem to figure out what separates them. Thanks for all of your help in advance!

EDIT: I am also looking for a good, free of charge, PHP editor. If it can work with other languages, that would be great. The more the merrier!
Last edited by HungryMind on Sun Jun 22, 2003 1:29 pm, edited 1 time in total.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

\n doesn't work with displaying to a web browser.. put a break tag in there

Code: Select all

<?php


print $myname . " is " . $myage . " years old<BR>"; 
print "hello."

?>[\php]

if you're sending email or writing to a text file the \n will work as you expect
?>
User avatar
HungryMind
Forum Commoner
Posts: 41
Joined: Fri Jun 20, 2003 10:33 am
Location: Alameda, CA

Post by HungryMind »

Okay I'll just go back to using <br /> then.

To repeat my other two questions from above: What is the difference between "\n" and "\r"? Are there any good php editors that I can be pointed towards? Thanks again.
Last edited by HungryMind on Sun Jun 22, 2003 1:29 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

\n puts a new-line into the HTML source. If you view source on your page you will see the new-line.

Newlines are, however, ignored by the browser rendering engines. To make the browser seperate lines you have to use <br />'s.

Most code I write will use both such as

Code: Select all

echo "$myname is $myage years old.<br />\n";
echo "Hello. <br />\n";
This way it looks right both on the browser and in the XHTML source. Keeping the XHTML source clean with line-breaks is very useful when you have to debug why something isn't displaying the way you wanted it to.
User avatar
m@ndio
Forum Regular
Posts: 163
Joined: Fri Jun 06, 2003 12:09 pm
Location: UK

Post by m@ndio »

hi,

the \n doesn't put a <br> in the html it simply inserts a newline in the code like this:

hello
----new line----

try this:

Code: Select all

print $myname . " is " . $myage . " years old.\n<br>"; 
print "hello."
diehuman
Forum Newbie
Posts: 5
Joined: Tue Jun 17, 2003 4:09 pm
Location: Kalispell, MT

Post by diehuman »

HungryMind wrote:Okay I'll just go back to using <br /> then.

To repeat my other two questions from above: What is the difference between "\n" and "\r"? Are there any good php editors that I can be pointed towards? Thanks again.
Nice to see we are all XHTML compliant today :)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

\n is a new line and \r is a carraige (sp?) return. In *nix lines are ended by \n, in Windows \r\n and on Macs (I believe) \r.

For PHP editors:
viewtopic.php?t=6288

Mac
User avatar
releasedj
Forum Contributor
Posts: 105
Joined: Tue Jun 17, 2003 6:35 am

Post by releasedj »

New lines and carriage returns come from the age of the typewriter.
New lines (\n) represents the paper being moved up to a new line, the carriage return (\r) represent when the carriage got to the end of the line and had to be moved back to the beginning.

This is good to keep in mind when using some of the image functions in PHP.
User avatar
HungryMind
Forum Commoner
Posts: 41
Joined: Fri Jun 20, 2003 10:33 am
Location: Alameda, CA

Post by HungryMind »

Thank you all for your help, I really appreciate you taking your time to help me.
Post Reply