difference between ' and "?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

difference between ' and "?

Post by method_man »

ive noticed in php that sometimes people use ' and sometimes people use ". when should you use which one?

for example

Code: Select all

$apple = 'ripe'
or

Code: Select all

$apple = "ripe"
Last edited by method_man on Wed Dec 21, 2005 11:18 pm, edited 1 time in total.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: difference between ' and "?

Post by Roja »

method_man wrote:ive noticed in php that sometimes people use ' and sometimes people use ". when should you use which one?
Use single quotes, unless you need a variable parsed:

Code: Select all

$apple = 'ripe';
$fruit = "The apple is $apple";
Although personally, I generally seperate variables, avoiding the need for doublequotes:

Code: Select all

$apple = 'ripe';
$fruit = 'The apple is ' . $apple;
method_man
Forum Contributor
Posts: 257
Joined: Sat Mar 19, 2005 1:38 am

Post by method_man »

thanks :D
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: difference between ' and "?

Post by josh »

Roja wrote: Use single quotes, unless you need a variable parsed:
Can you give a reason why? What's the downside of using double quotes all the time, unless you need to insert a variable literally but that can be escaped
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

There is a whole conversation about that here:

viewtopic.php?t=19006&postdays=0&postorder=asc&start=0

I belive the single vs double quote conversation starts around page 3 or 4.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: difference between ' and "?

Post by Roja »

jshpro2 wrote:Can you give a reason why? What's the downside of using double quotes all the time, unless you need to insert a variable literally but that can be escaped
It avoids the variable being parsed, which can help protect you against unintentional miscoding.

My preference is to not have variables parsed unless I explicitly tell it to.

Of course, as time goes on, I use echo/print/inline out put far less often, relying on templates for output. As a result, its less of a concern for me.

Its definitely a preference thing. I can see strong arguments for the counter position as well.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

I use:

Code: Select all

echo "Lets echo a variable here: ".$variable.", and output a function here: ".date();
It suits me.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

onion2k wrote:I use:

Code: Select all

echo "Lets echo a variable here: ".$variable.", and output a function here: ".date();
It suits me.
IMHO ~ As long as you find a standard and stick to it. Single vs Double is quite trivial. However, with that said; here is my standard :wink:

I prefer to use single quotes

Using onion2k example:

Code: Select all

echo 'Lets echo a variable here: '.$variable.', and output a function here: '.date();
1. I know for a fact that there are NO variables in the string 'Lets echo a variable here: ' because they are enclosed in single quotes. As a matter of fact if I were going through thousands of lines of code, I could easily skip over that string knowing its just a string and not a string parsed with a variable.

It really comes down to coding style and what a coder is comfortable in doing. In a prior job a did a bunch of code review, it was much easier for me to read through strings that were single vs double.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Me too.

I just use whatever feels best.

A few examples of how I choose single or double quotes:

Code: Select all

echo '<a href="http://www.website.com">Click Me</a>';
Here I would use single quotes, because I'm used to coding HTML with double quotes. If I used the echo statement with double quotes instead of single, I would have to escape them each time, or use single quotes in the HTML instead, which look weird to me because I'm not used to it.

Code: Select all

echo "Name: $name\n";
This is a prime example of double quote usage, If single quotes were used here instead of double quotes, the page would read "Name: $name\n" instead of "Name: [$name's value] [new line]".

Like onion said, it's mainly down to preference, also like onion, I have got used to seperating variables from my statements anyway.

So even though:

Code: Select all

echo "Name: $name\n";
is quicker and easier than:

Code: Select all

echo "Name: " . $name . "\n"; //or echo "Name: ", $name, "\n";
I would still use the latter.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Moved to Theory and Design.
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post by AGISB »

I used to use double quotes but at one time I moved to single.

I was told to overwork a php project that was poorly coded in the html output. I was told to change the html so it is valid. Now I had to add "" around all html options like width="100%". So I had to rather escape all " or change the string outline to ' . No matter what it ment additional work for me so I code with single quotes to avoid such hassle ever again.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

/me is a single quoter...;)

It just seems to avoid trouble further down the line - so to me at least it makes sense. There is method to this madness. Its easier when you double quote HTML attribute values, and split variables from strings and use simple concatenation (which also incidentally keeps the variables highlighted on any colour coded syntax used by an editor.
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Maugrim_The_Reaper wrote: ... (which also incidentally keeps the variables highlighted on any colour coded syntax used by an editor.
Damn good point! I pretty much do it the same way but I hadn't thought about that extra bit of goodness.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

But it's annoying when you're writing a sentence.

Code: Select all

$string = 'But it's annoying when you're writing a sentence.';
Parse error! ARGH!
Post Reply