473,572 Members | 3,174 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16404
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****@donotsp am.remove.me.he raldic.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****@donotsp am.remove.me.he raldic.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.

doublequotestim e = 0;
singlequotestim e = 0;

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

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

echo doublequotestim e;
echo singlequotestim e;

--
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.g nilink.net>, Joshua Beall
<jb****@donotsp am.remove.me.he raldic.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=microtim e();
## end timing

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

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

echo "singleperr un: $singleperrun (over $singleruns runs)\n";
echo "doubleperr un: $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****@donotsp am.remove.me.he raldic.us> schrieb im
Newsbeitrag news:67******** ********@nwrddc 03.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=microtim e();
## end timing

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

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

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

echo "singleperr un: $singleperrun (over $singleruns runs)\n";
echo "doubleperr un: $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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4749
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 varaible in a string. This is fine, but having to remember to put an r in front of regex's is really annoying and it would be great if you could jsut...
4
49504
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
1471
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 = '" & cfName.Trim & "' Where id = " & lnID the command works fine unless and until clname and/or cfname contains an apostrophe, ie: O'Hare When the...
4
31200
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"> </Authors>
7
20906
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 comes in when you copy a double quote from MS Word and paste it in the text box. What happens is the double quote becomes slanted (“) so my code above...
2
18682
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 onMouseOver='return overlib(\"<A HREF=\'javascript:void(0)\' onClick=\'javascript:getXMLFeed(\"price\")\'>Price</A>\"'>"; How do you do a 5th level...
8
21473
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: "SomeNode[SomeAttribute = 'abc@apos;s search'"
4
6237
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 the field has a single quote in it like "O'Toole", how can I construct the string ? Do While drSQL.Read sSQL = "insert into " & sTableName & "...
1
2955
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 expression for my string test.
0
7733
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7957
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7705
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1240
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
975
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.