Page 1 of 2

difference between ' and "?

Posted: Wed Dec 21, 2005 11:17 pm
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"

Posted: Wed Dec 21, 2005 11:18 pm
by josh

Re: difference between ' and "?

Posted: Wed Dec 21, 2005 11:22 pm
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;

Posted: Wed Dec 21, 2005 11:40 pm
by method_man
thanks :D

Re: difference between ' and "?

Posted: Thu Dec 22, 2005 9:17 am
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

Posted: Thu Dec 22, 2005 9:23 am
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.

Re: difference between ' and "?

Posted: Thu Dec 22, 2005 9:24 am
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.

Posted: Thu Dec 22, 2005 9:31 am
by onion2k
I use:

Code: Select all

echo "Lets echo a variable here: ".$variable.", and output a function here: ".date();
It suits me.

Posted: Thu Dec 22, 2005 9:57 am
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.

Posted: Thu Dec 22, 2005 10:01 am
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.

Posted: Thu Dec 22, 2005 10:10 am
by hawleyjr
Moved to Theory and Design.

Posted: Fri Dec 23, 2005 12:27 am
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.

Posted: Fri Dec 23, 2005 3:51 am
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.

Posted: Fri Dec 23, 2005 11:07 am
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.

Posted: Fri Dec 23, 2005 2:09 pm
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!