473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 16391
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Simon Bunker | last post by:
I was just wondering why python doesn't make a distinction between single and double quotes - a bit like Perl does. Obviously I realise there are no dollar signs so you can't intrpolate a...
4
by: Thomas Miskiewicz | last post by:
Hi! Is using of a double quotation mark with a URL a problem? For example: http://myserver.com/query?field1=something&field2=test&params="field1=test1"+"field2=test2" Regards Thomas
2
by: David Bird | last post by:
I am attempting to build a command string to update a field in a dataset. the command string looks like this: Dim cmdString As String = "Update set lName = '" & clName.Trim & "', fName = '" &...
4
by: Greg | last post by:
I keep getting an error when I have a tick mark in a text value that I am searching for in my XPath Query. Example: <Authors> <Author LastName="O'Donnel"> <Author LastName="Smith">...
7
by: gar | last post by:
Hi, I need to replace all the double quotes (") in a textbox with single quotes ('). I used this code text= Replace(text, """", "'" This works fine (for normal double quotes).The problem...
2
by: Dustin | last post by:
I have a nested quotes issue I'm trying to resolve. Basically I need a 5th level nested quote for quoting the string argument to the getXMLFeed() function in the following: var myHTML = "<TR...
8
by: Marina Levit [MVP] | last post by:
I've scoured google, but apparently none of the suggestions actually work. I have the following. type of XPATH query "SomeNode[SomeAttribute = 'abc's search'" Now, I've tried doing this: ...
4
by: fniles | last post by:
I am looping thru DataReader and constructing a sql query to insert to another database. When the data type of the field is string I insert the field value using a single quote. When the value of...
1
by: U Aye Thein | last post by:
I found in internet how to solve single quotation mark in string and how to solve double quotation mark in string but my string may be contained single quote or double quote. How to write an...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.