Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

New to the forum and php...

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

New to the forum and php...

Postby eyewoo » Sun Mar 09, 2003 10:53 am

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... :?:
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby Xerpher » Sun Mar 09, 2003 11:05 am

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: Select all
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/
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Sun Mar 09, 2003 11:10 am

Helps a lot!!! Thanks for the quick reply... :D
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA

Postby pootergeist » Sun Mar 09, 2003 11:20 am

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.
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby Xerpher » Sun Mar 09, 2003 11:37 am

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)
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby eyewoo » Sun Mar 09, 2003 11:57 am

Cool... got it... :)
eyewoo
New php-forum User
New php-forum User
 
Posts: 71
Joined: Sun Mar 09, 2003 10:29 am
Location: Pennsylvania, USA


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 8 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron