Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single quotes or double quotes for variable concatenation? [closed]

Tags:

Is it better to concatenate a variable (say, $name) into an existing string (say, $string) like this:

$string='Hi, my name is '.$name 

or to embed the variable in the string like this:

$string="Hi, my name is $name"; 

or is it better to use a function like this:

$string=sprintf("Hi, my name is %s",$name); 

Which is better in terms of processor time/efficiency?

like image 448
ina Avatar asked Jul 23 '10 07:07

ina


People also ask

Which variable is enclosed in single quotes?

In C and C++ the single quote is used to identify the single character, and double quotes are used for string literals. A string literal “x” is a string, it is containing character 'x' and a null terminator '\0'.

Which form of data is enclosed with single and double quotes?

String is enclosed either with single quotes or double quotes.

Can string be enclosed in single quotes?

Both quotes represent Strings but be sure to choose one and STICK WITH IT. If you start with a single quote, you need to end with a single quote. There are pros and cons to using both IE single quotes tend to make it easier to write HTML within Javascript as you don't have to escape the line with a double quote.

Why is concatenate adding quotes?

When you concatenate text, you surround the text with double quotation marks so Microsoft Excel recognizes it as text. Otherwise, you'll receive an error. Excel then uses the text within quotes but discards the quotation marks.


2 Answers

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)

like image 164
Blizz Avatar answered Sep 25 '22 18:09

Blizz


The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

'\'' 

While if you use double quotes you have to escape other characters:

"\$" 

But it also allows for some nifty things like adding a new-line to the end:

"my string\n" 

With single quotes you would have to do a concatenation:

'my string' . chr(10) 'my string' . "\n" 

Generally, single quotes are faster because they are "dumb".

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

Consider this:

$str1 = 'asdf'; $str2 = 'qwer';  // this will result in more memory being allocated for temporary storage echo $str1 . $str2;  // this will not allocate as much memory as the previous example echo $str1; echo $str2; 
like image 37
thnee Avatar answered Sep 22 '22 18:09

thnee