New to the forum and php...

A

Anonymous

Guest
I'm and illustrator who has used javascript to control his website. I now need some more capabilities, so I'm picking up PHP. I've been coding for about 20 years in various languages, so I have a pretty good grounding to begin with.

However, I've immediately run into a problem...?
And it's just with the simple, first, "Hello World!" script.

The problem has to do with the single and double quote marks, and the line break symbol.

These lines:
<?php print('Hello, World!\n ...Hello, again\n');?>
<br>
<?php print("Hello, World!\n ...Hello, again\n");?>


Display like this:
Hello, World!\n ...Hello, again\n
Hello, World! ...Hello, again


The first PHP line, which uses a single quote symbol in the print string displays the \n rather than sending a line break.

The second PHP line, which uses the double quote symbol, doesn't display the \n, but it also doesn't do a line break.

Any idea what the problem is.... and it seems to me that the double and single quotes should act the same way...

Confused... :?:
 
Single Quote takes your string literally and won't replace things in it with predefined information. Double Quotes however do, and actually, yes, your lines ARE being seperated (look in the source code) if you want it on a new line on the page people see then either replace \n with a <br> or just put the <br> beside the \n. \n only spits out a newline in the source. Or you could just use a lot of \n's then use the function nl2br() like this:

Code:
print(nl2br("Hello, World!\n...Hello, again\n...And again\n"));
nl2br() keeps the newline commands in the source and adds appropriate HTML newline commands.

Any of those methods will get the desired results :) Hope this helps.

BTW: Your an amazing artist if you made those pictures at http://www.eyewoo.com/
 
on a subnote:

outputting data (print or echo) with single quotes can accomodate hard typed line breaks

echo 'line 1
line 2
line 3';

which the receiving OS handles as either \n (M$) or \n\r (Mac)

useful for code output structuring.
 
And also with what pootergeist said, that works for both single and double quotes, not just single :)

(At least on Win and Unix systems, never worked on a Mac before)
 
Back
Top