469,271 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Single versus Double quote marks as string delimiters

Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings (e.g.,
'Hello, World'). This seems backwards to me, since double quote strings
have to be checked for any variables that need to be interpreted, whereas
single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks for any
information!

-Josh
Jul 17 '05 #1
12 16155
Joshua Beall:
Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings
(e.g.,
'Hello, World'). This seems backwards to me, since double quote strings
have to be checked for any variables that need to be interpreted, whereas
single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks for
any information!


The truth is as you think, double quoted strings require more work. But the
difference is absolutely negligible. I use single or double quotes
depending on the nature of the text, if it contains many double quotes,
like HTML often does, I prefer single quotes.

André Nęss
Jul 17 '05 #2
Joshua Beall <jb****@donotspam.remove.me.heraldic.us> wrote:
I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings
(e.g., 'Hello, World'). This seems backwards to me, since double
quote strings have to be checked for any variables that need to be
interpreted, whereas single quoted strings do not.


http://www.phpfreaks.com/forums/inde...howtopic=13321 (a reliable
source) says that single quotes are prefereable.
--
Michael Wilcox
mjwilco at yahoo dot com
Essential Tools for the Web Developer - http://mikewilcox.t35.com
Jul 17 '05 #3
On 2004-01-24, Joshua Beall <jb****@donotspam.remove.me.heraldic.us> wrote:
Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings (e.g.,
'Hello, World'). This seems backwards to me, since double quote strings
have to be checked for any variables that need to be interpreted, whereas
single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks for any
information!


Imho there is only one way to find out. experiment.

doublequotestime = 0;
singlequotestime = 0;

for 10000 times
get timestamp
for 1000000000 times
echo "hello world"
get timestamp2
doublequotestime += (timestamp2 - timestamp)

get timestamp
for 1000000000 times
echo 'hello world'
get timestamp2
singlequotestime += (timestamp2 - timestamp)

echo doublequotestime;
echo singlequotestime;

--
http://home.mysth.be/~timvw
Jul 17 '05 #4
Joshua Beall wrote:
Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings
(e.g., 'Hello, World'). This seems backwards to me, since double
quote strings have to be checked for any variables that need to be
interpreted, whereas single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks
for any information!


Strings enclosed in single quotes are taken literally, double quoted strings
are parsed by PHP:

$w = "World";
print "Hello $w"; // prints `Hello World`
print 'Hello $w'; // prints `Hello $w`

So in that sence, single quoted strings are faster and you're are right...
JW

Jul 17 '05 #5
In message <67****************@nwrddc03.gnilink.net>, Joshua Beall
<jb****@donotspam.remove.me.heraldic.us> writes
Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings (e.g.,
'Hello, World'). This seems backwards to me, since double quote strings
have to be checked for any variables that need to be interpreted, whereas
single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks for any
information!


I heard the other way round, but have no idea just what kind of impact
it actually makes. My suspicion is that in the real world very little,
unless the server is almost at breaking point.

--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #6
Tim Van Wassenhove wrote:
Imho there is only one way to find out. experiment.


single quotes are marginally faster (as has been said in this thread
often enough)

#v+
<?php
$doubletime=0;
$singletime=0;
$doubleruns=0;
$singleruns=0;

$n=100000;
while (--$n) {
$double=true;
if (rand()%2) $double=false;

## start timing
$start = microtime();
## every time the script goes through here
## it will do two comparisons and one assignment
if ($double) $x="n=$n";
if (!$double) $x='n='.$n;
$finis=microtime();
## end timing

## Is microtime()'s resolution accurate enough for this test? ????!?!?

if ($double) $doubletime+=array_sum(explode(' ', $finis)) - array_sum(explode(' ', $start));
else $singletime+=array_sum(explode(' ', $finis)) - array_sum(explode(' ', $start));
if ($double) ++$doubleruns;
else ++$singleruns;
}
$doubleperrun=$doubletime/$doubleruns;
$singleperrun=$singletime/$singleruns;

echo "singleperrun: $singleperrun (over $singleruns runs)\n";
echo "doubleperrun: $doubleperrun (over $doubleruns runs)\n\n";
?>
#v-

On my machine one run of this script produced:
singleperrun: 3.76346098519E-05 (over 49817 runs)
doubleperrun: 4.15038085678E-05 (over 50182 runs)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #7
:-)))
parsing single quoted strings is naturally FASTER, and not vice versa. If
you don't believe
just run a small test (5 lines script, timer on, loop, some evaluation, end
of loop, timer off, compare time). Then change the evaluation part and
compare the times again. (or just read something on iteration counting and
you will find out that it is not true what those people say, (unless php
coders intended it, what they naturally did NOT :-)))

Best Regards,

Lucas
"Joshua Beall" <jb****@donotspam.remove.me.heraldic.us> schrieb im
Newsbeitrag news:67****************@nwrddc03.gnilink.net...
Hi All,

I have heard other people say that PHP can parse double quoted strings
(e.g., "Hello, World") faster than it can parse single quoted strings (e.g., 'Hello, World'). This seems backwards to me, since double quote strings
have to be checked for any variables that need to be interpreted, whereas
single quoted strings do not.

So, what is the truth on this matter? And the explanation? Thanks for any information!

-Josh

Jul 17 '05 #8
On 2004-01-24, Pedro Graca <he****@hotpop.com> wrote:
Tim Van Wassenhove wrote:
Imho there is only one way to find out. experiment.


single quotes are marginally faster (as has been said in this thread
often enough)


I wrote the post even after why ppl had explained which is the fastest

All i was trying to say to the OP: measure the differences, and see if
you think they are big enough to care about them. In most cases the
bottleneck is not in the difference between single and double quotes
anyway.
--
http://home.mysth.be/~timvw
Jul 17 '05 #9
Your test code has too much overhead. I ran the code listed below and the
loop with the single quoted string finishes in less than half the time the
double quoted one does.

Who cares about microsecond differences though. If you do, then consider
contracting the names of your functions to something like f1(). Calling
functions with longer names is slightly more costly.

function microtime_diff($a, $b) {
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
return $b_sec - $a_sec + $b_dec - $a_dec;
}

$something = "Cow";

$start_time_1 = microtime();
for($i = 0; $i < 1000000; $i++) {
$s = "This is $something else";
}
$duration_1 = microtime_diff($start_time_1, microtime());

$start_time_2 = microtime();
for($i = 0; $i < 1000000; $i++) {
$s = 'This is ' . $something . 'else';
}
$duration_2 = microtime_diff($start_time_2, microtime());

echo "$duration_1 vs. $duration_2";

Uzytkownik "Pedro Graca" <he****@hotpop.com> napisal w wiadomosci
news:bu************@ID-203069.news.uni-berlin.de...
Tim Van Wassenhove wrote:
Imho there is only one way to find out. experiment.
single quotes are marginally faster (as has been said in this thread
often enough)

#v+
<?php
$doubletime=0;
$singletime=0;
$doubleruns=0;
$singleruns=0;

$n=100000;
while (--$n) {
$double=true;
if (rand()%2) $double=false;

## start timing
$start = microtime();
## every time the script goes through here
## it will do two comparisons and one assignment
if ($double) $x="n=$n";
if (!$double) $x='n='.$n;
$finis=microtime();
## end timing

## Is microtime()'s resolution accurate enough for this test? ????!?!?

if ($double) $doubletime+=array_sum(explode(' ', $finis)) -

array_sum(explode(' ', $start)); else $singletime+=array_sum(explode(' ', $finis)) - array_sum(explode(' ', $start)); if ($double) ++$doubleruns;
else ++$singleruns;
}
$doubleperrun=$doubletime/$doubleruns;
$singleperrun=$singletime/$singleruns;

echo "singleperrun: $singleperrun (over $singleruns runs)\n";
echo "doubleperrun: $doubleperrun (over $doubleruns runs)\n\n";
?>
#v-

On my machine one run of this script produced:
singleperrun: 3.76346098519E-05 (over 49817 runs)
doubleperrun: 4.15038085678E-05 (over 50182 runs)
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--

Jul 17 '05 #10
In message <bu************@ID-203069.news.uni-berlin.de>, Pedro Graca
<he****@hotpop.com> writes
Tim Van Wassenhove wrote:
Imho there is only one way to find out. experiment.
single quotes are marginally faster (as has been said in this thread
often enough)

<snip>
On my machine one run of this script produced:
singleperrun: 3.76346098519E-05 (over 49817 runs)
doubleperrun: 4.15038085678E-05 (over 50182 runs)


That is *very* marginal. I shan't worry about what kind of quotes I use
now!
--
Five Cats
Email to: cats_spam at uk2 dot net
Jul 17 '05 #11
If you're sending output to the page, the best string delimiters are ?>
and <?php.
Jul 17 '05 #12
On Sat, 24 Jan 2004 18:55:20 +0000, André Nęss wrote:
I use single or double quotes
depending on the nature of the text, if it contains many double quotes,
like HTML often does, I prefer single quotes.


Why not use single quotes in your HTML attributes? That way you can print
HTML with variables. Perfectly valid too [1].

Cheers,
Andy
[1] http://www.w3.org/TR/html4/intro/sgm...tml#attributes
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Simon Bunker | last post: by
4 posts views Thread by Thomas Miskiewicz | last post: by
2 posts views Thread by David Bird | last post: by
8 posts views Thread by Marina Levit [MVP] | last post: by
4 posts views Thread by fniles | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.