Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is better on performance: double quoted strings with variables or single quoted strings with concatenations?

I know that using single quotes around a string in PHP is faster than using the double quotes because PHP doesn't need to check for variable presence in the single quoted string. My question is which will perform better:

A) A double quoted string with variables present:

echo "foo bar $baz";

or

B) Single quoted with a concatenated variable:

echo 'foo bar ' . $baz;
like image 625
Steven Avatar asked Aug 21 '09 11:08

Steven


People also ask

Which is better to use with a print function double quotes or single quotes Why?

# this code prints the output within quotes. The choice between both the types (single quotes and double quotes) depends on the programmer's choice. Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL.

Which quote is evaluated faster in PHP?

If no processing of the text within is required then single is faster.

Is it better to use single quotes or double quotes in Javascript?

Both single (' ') and double (" ") quotes are used to represent a string in Javascript. Choosing a quoting style is up to you and there is no special semantics for one style over the other. Nevertheless, it is important to note that there is no type for a single character in javascript, everything is always a string!

Should I use single or double quotes react?

A few repositories of popular JavaScript projects reveals that single quotes are favored over double quotes. You can see that front-end libraries (React, Angualar) have more double quotes than the other libraries as might have to do with the presence of HTML fragments.


2 Answers

I did a benchmark of this on a blog I was working on a while ago. However, as I've come to realize there are a lot of variables. Chief among them are:

  1. How many concatenations are you doing? Each time you do a concatenation PHP re-parses the entire string (or so I've been told). So 1 concatenation may be faster, but 6 may be considerably slower.
  2. Data type. Though I haven't tested this one myself personally I've been told that the data type being concatenated matters as well, though I'm not sure how much.

Over all I'd say it really isn't that big of a deal for you to actually worry about it. Generally speaking it's only going to make a noticeable difference if you're writing a huge site (think MySpace, Facebook, Flickr, etc) and usually by that point you have so much hardware behind you that the single vs double quotes thing becomes irrelevant again.

Personally I'd say there's far more important things that will impact performance in a much more substantial way (caching, sql optimization, auto loading to prevent unnecessary includes, etc).

I personally choose single quotes nearly every time, but not for speed. I do because I think it's more readable. And that to me is important.

like image 58
Steven Surowiec Avatar answered Nov 02 '22 08:11

Steven Surowiec


In all honesty, I don't think you need to be scrutinizing this particular convention when looking to save performance. But, for what it's worth, I think I recall reading that variables within strings are less-efficient (mind you, it's negligible and in my opinion not an issue) than concatenation.

like image 24
Sampson Avatar answered Nov 02 '22 10:11

Sampson