Single Quotes or Double Quotes for Variable Concatenation

Single quotes or double quotes for variable concatenation?

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)

Two variables concatenated inside single quotes

There is no difference between a variable declared with single quotes vs. one declared with double quotes:

var a = 'test';
var b = "test";
a === b;
> true

To join your two strings, just concatenate them with a + or concat():

var x='http://www.xyzftp/myservice/service1.svc';
var y='/logincheck';

var z = x + y; // concat with +
z;
> "http://www.xyzftp/myservice/service1.svc/logincheck"

// or

var z = x.concat(y); // concat with contact()
z;
> "http://www.xyzftp/myservice/service1.svc/logincheck"

Now, if we're all just misunderstanding your question, and what you are actually looking for is a string with single quotes as part of the string contents, here's how you can get that:

var x='http://www.xyzftp/myservice/service1.svc';
var y='/logincheck';
var z = "'" + x + y + "'"; // use double quotes as string delimiters
// or:
var z = '\'' + x + y + '\''; // use single quotes as delimiters and
// escape the single quote in the string
z;
> "'http://www.xyzftp/myservice/service1.svc/logincheck'"

PHP basic concatenation issue?

Why is unnecessary to escape double quotes when we are inside a string? Because double quotes will never be taken as anything else then a string, if they are inside single quotes?

The manual, regarding single quoted strings:

To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.

What is the difference between single-quoted and double-quoted strings in PHP?

PHP strings can be specified not just in two ways, but in four ways.

  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

When should you use single or double quotes in PHP?

The only difference is that double quoted strings interpret embedded variables and a number of escape sequences, while single quoted strings do not. E.g.:

'This is $a \n string'

This is literally the string "This is $a \n string".

"This is $a \n string"

This is a string containing the value of variable $a and a line break.

Use both as appropriate. If neither escape sequences nor variables are of interest to you, I'd default to single quoted strings; with one exception: if you need single quotes in the string, then double quotes are "cleaner". Compare:

'I don\'t care.'
"I don't care."

concat php strings; using . operator or double quotes

I think before you start worrying about it, you need to see if it is even worth thinking about. I did think about it, and wrote the following tiny script and ran it to see what the benchmarks were like.

For each loop, I made 100,000 passes. Now I didn't print my strings anywhere so if the PHP optimizer takes all of my work away because of that, then I apologize. However looking at these results, you are looking at a difference of about 0.00001 second for each.

Before you optimize for anything other than readability, use a profiler and see where your hotspots are. If you run tens of millions of concatenations, then you may have an argument. But with 1000, you are still talking about a difference of 0.01 seconds. I'm sure you could save more than 0.01 seconds just by optimizing SQL queries and the like.

My evidence is below....

Here's what I ran:

<?php
for($l = 0; $l < 5; $l++)
{
echo "Pass " .$l. ": \n";
$starta = microtime(1);
for( $i = 0; $i < 100000; $i++)
{
$a = md5(rand());
$b = md5(rand());
$c = "$a $b".' Hello';
}
$enda = microtime(1);

$startb = microtime(1);
for( $i = 0; $i < 100000; $i++)
{
$a = md5(rand());
$b = md5(rand());
$c = $a . ' ' . $b . ' Hello';
}
$endb = microtime(1);

echo "\tFirst method: " . ($enda - $starta) . "\n";
echo "\tSecond method: " . ($endb - $startb) . "\n";
}

Here are the results:

Pass 0: 
First method: 1.3060460090637
Second method: 1.3552670478821
Pass 1:
First method: 1.2648279666901
Second method: 1.2579910755157
Pass 2:
First method: 1.2534148693085
Second method: 1.2467019557953
Pass 3:
First method: 1.2516458034515
Second method: 1.2479140758514
Pass 4:
First method: 1.2541329860687
Second method: 1.2839770317078


Related Topics



Leave a reply



Submit