Forum Moderators: coopster

Message Too Old, No Replies

Practical reason for Single Quotes vs Double Quotes

and vice/versa

         

neophyte

7:54 am on Sep 25, 2006 (gmt 0)

10+ Year Member



I've always wondered why - or even if there's a difference - for using double quotes vs single quotes in php.

My code typically ends up with a mish-mash of both - sometimes I'll use:

$tomorMonth = date('n', $tomorrow); on one line, and then,
$arrMonth = $_POST['arrive_month']; on another line (or lines)

Both seem to work okay - I don't seem to be throwing any errors - but my question here lies in the interest of getting the "best practices" usage straight so I can start implementing single or doubles as appropriate.

So... when SHOULD someone use singles over doubles ... or does it make any difference to php at all?

Neophyte

HoboTraveler

8:19 am on Sep 25, 2006 (gmt 0)

10+ Year Member



I could be wrong, but I understand that single quotes use less memory or resources.

I use the double quotes only when echoing variables as single quotes do not work in such cases.

siMKin

8:21 am on Sep 25, 2006 (gmt 0)

10+ Year Member



this has been discussed so many times already, that i think it's much better to search for it, then start a new one.
[google.com...]

jatar_k

3:16 pm on Sep 25, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you could try this thread
Benchmarking PHP text output [webmasterworld.com]

other reasons

I use double quotes when i am building queries because queries use single quotes

I use single quotes the rest of the time as I echo using the comma and never have variables resolve inside my quotes. This also helps for html as html uses double quotes

using strictly single quotes will help your syntax, arrays don't resolve inside double quotes unless you use braces around them. There are some common errors that people make when putting vars inside double quotes for queries. never having vars resolve inside strings helps you not make these common syntax errors

HoboTraveler

6:35 am on Sep 26, 2006 (gmt 0)

10+ Year Member



@jatar_k,

Could you please explain with an example.. Are you saying that variables should not be echoed when displaying output?

TIA

phparion

11:57 am on Sep 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Single quotes are faster to execute as compare to double quotes. but the differene is neglegible for simple statements.

the double quotes are parsed by php to find where there is any variable that needs to be displayed e.g

$w = "world";

echo " Hello $w"; // taken as Hello World

echo 'Hello $w'; // taken as Hello $w

however, I have noticed that sometimes some escape characters don't work properly with single quotes.

jatar_k

3:04 pm on Sep 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I use double quotes for queries but always concatenate my vars

$sql = "select mycol from mytable where mycond='" . $myvar . "'";

I use single quotes for everything else

echo '<a href="somepage.php">my link</a>';

and I use commas in my echo statements instead of concatenation because it is faster

echo '<p>this shows var1: ',$var1,'<br>and var2: ',$var2,"\n";

obviously you need to use double quotes for some things, such as making newlines resolve properly

neophyte

2:59 am on Sep 27, 2006 (gmt 0)

10+ Year Member



Also, and BTW, since I started this thread and realized the reason to use singles over doubles (everwhere) I've found a massive benefit that most already probably know.

I include it here for the other newb's like myself to benefit from:

Where I use to have to escape EVERYTHING using double quotes ...

echo "<a href=\"somepage.php\">my link</a>";

Now, as Jatar points out, it's just:

echo '<a href="somepage.php">my link</a>';

... what an unbelievable time (and debugging-hassle) saver!

I didn't know that you could use commas in an echo either - I've always concatenated my vars in an echo... but not anymore!

Thanks to all!

Neophyte

jatar_k

5:19 am on Sep 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> what an unbelievable time (and debugging-hassle) saver!

hehe, darn right. I try not to do things that make my life more difficult

coders are lazy ;)

RonPK

8:40 am on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



arrays don't resolve inside double quotes unless you use braces around them.

$arr = array('bar', 'bar2'); 
echo "foo $arr[0]";

That will print

foo bar
.

You can also retrieve named keys like $_GET['query'] inside double quotes: just leave out the single quotes.

jatar_k

3:02 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> just leave out the single quotes

but that would actually be incorrect syntax

RonPK

4:00 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I know, but it works and saves some typing ;)

jatar_k

4:01 pm on Sep 27, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe

Vali

9:12 pm on Sep 27, 2006 (gmt 0)

10+ Year Member



The only difference that is of any use:

print '\n'; # prints \n
print "\n"; # prnts a new line.

The rest is just preference...