Page 1 of 1

\n problem

Posted: Fri Jul 27, 2012 6:18 am
by global_erp_solution
AFAIK, \n is global convention for newline. but

Code: Select all

echo 'a\nb';
produces a\nb
and

Code: Select all

echo "a\nb";
produces ab

Can anybody explain this behavior? and how can one print newline character? I've always used <br>. thanks

Re: \n problem

Posted: Fri Jul 27, 2012 6:51 am
by Celauran
If you look at the HTML source, you'll see a new line between a and b. As you mentioned, though, HTML for newline is

Code: Select all

<br />
.

Re: \n problem

Posted: Fri Jul 27, 2012 7:37 am
by Grizzzzzzzzzz
global_erp_solution wrote:AFAIK, \n is global convention for newline. but

Code: Select all

echo 'a\nb';
produces a\nb
and

Code: Select all

echo "a\nb";
produces ab

Can anybody explain this behavior? and how can one print newline character? I've always used <br>. thanks
single quotation marks indicate to PHP that you want it to treat the string literally, variables and escape sequences will not be parsed and the string is simply outputted as it is (the exception being when using backslashes to escape)

strings in double quotation marks are parsed for variables and the like, and so sequences such as \n or &nbsp; are interpreted.

Re: \n problem

Posted: Fri Jul 27, 2012 1:35 pm
by requinix
Grizzzzzzzzzz wrote:single quotation marks indicate to PHP that you want it to treat the string literally, variables and escape sequences will not be parsed and the string is simply outputted as it is (the exception being when using backslashes to escape)
Backslashes stay with only two exceptions: \' and \\.

Code: Select all

echo '\a\p\o\s\t\r\o\p\h\e\:\ \'\,\ \b\a\c\k\s\l\a\s\h\:\ \\';

Code: Select all

\a\p\o\s\t\r\o\p\h\e\:\ '\,\ \b\a\c\k\s\l\a\s\h\:\ \
Grizzzzzzzzzz wrote:strings in double quotation marks are parsed for variables and the like, and so sequences such as \n or &nbsp; are interpreted.
PHP does not interpret HTML entities.

Re: \n problem

Posted: Mon Jul 30, 2012 3:28 am
by Grizzzzzzzzzz
requinix wrote:
Grizzzzzzzzzz wrote:strings in double quotation marks are parsed for variables and the like, and so sequences such as \n or &nbsp; are interpreted.
PHP does not interpret HTML entities.
oops :oops: my derp