Page 1 of 1

Problem with Escape Sequences, unclear on how to implement

Posted: Fri Jun 20, 2003 10:33 am
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!

Posted: Fri Jun 20, 2003 10:46 am
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
?>

Posted: Fri Jun 20, 2003 10:48 am
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.

Posted: Fri Jun 20, 2003 10:50 am
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.

Posted: Fri Jun 20, 2003 10:56 am
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."

Posted: Fri Jun 20, 2003 1:49 pm
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 :)

Posted: Fri Jun 20, 2003 3:43 pm
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

Posted: Fri Jun 20, 2003 4:57 pm
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.

Posted: Fri Jun 20, 2003 8:01 pm
by HungryMind
Thank you all for your help, I really appreciate you taking your time to help me.