Welcome Guest, Not a member yet? Register   Sign In
PHP double quotes versus single quotes
#1

[eluser]Andreas Bergström[/eluser]
I have a small issue I can't find any clear answer to. Simply, what of the following is faster:

Code:
echo "This is a string!";

or

Code:
echo 'This is a string!';

Now it seems logical to me that the single quotation should be faster since PHP doesn't process the string, but I've found sources around the web calling this a "legacy issue" that is no longer correct, and that double quotation always beats single quotation. I've run some own tests and double quotation actually seems faster? How can that be?

A similar example:

Code:
echo "Welcome, $fullname";

or

Code:
echo 'Welcome'.$fullname;

Here I also want to believe the later is the fastest since I'm giving PHP the variables straight away. But people tell me double quotation is faster even here, how?
#2

[eluser]Dam1an[/eluser]
Like you, I was led to beleive single quotes should be faster, as it doesnlt parse for variables, but also heard people arguing double quotes is faster, so not sure about that, but I do know that
Code:
echo 'Hello ', $world;
is quicker then
Code:
echo 'Hello '.$world;

(echo is a function which accepts multiple strings as parameters, and it's quicker to pass it multiple string them make it build up a single string using concatenation)
#3

[eluser]Nick Husher[/eluser]
Quote:2) "Use single-quotes for strings."
Benchmarks run against PHP 5.2 and 5.3 show that parsing double-quoted
strings with interpolation is no slower (and often faster) than single-
quoted strings using concatenation. When simple strings with no
variables in them are used, the performance is clearly better with
double-quoted strings due to implementation details in the engine. See
the benchmark posted at <http://pastie.org/523023>.
...
4) "Don't use concatenation with echo."
This is exactly the opposite of correct advice. The engine handles
multiple arguments to echo() in such a way that concatenation (or
double-quoted string interpolation) is actually much faster. See the
benchmark posted at <http://pastie.org/523020>.

From Gwynne Raskind, a PHP language developer. [source]
#4

[eluser]Adam Griffiths[/eluser]
The real fact of the matter is very simple. In some cases double quotes are faster and in other cases single quotes are faster. I would suggest you pick your own preference and stick with it.

Personally I use double quotes.
#5

[eluser]jedd[/eluser]
I'm with Adam - I use "these things" everywhere, possibly purely for historical reasons (every language I've used before, prefers quotes). I never use embedded variables within quoted strings, so the benefits of whichever ones let you do that aren't of interest to me. I have started to use apostrophes around array keys (eg. $value['foo']) because I think it looks a bit neater, but it's purely an aesthetic thing at that point.

Really, though, I'm hugely envious if you've explored all other avenues for optimising your algorithms and data structures, your schema and database queries, and are now focusing on the near-immeasurable differences between apostrophes and quotation marks in an attempt to squeeze out an extra dribble of performance from your system. Wink
#6

[eluser]brianw1975[/eluser]
[quote author="jedd" date="1250056199"]I'm with Adam - I use "these things" everywhere, possibly purely for historical reasons (every language I've used before, prefers quotes). I never use embedded variables within quoted strings, so the benefits of whichever ones let you do that aren't of interest to me. I have started to use apostrophes around array keys (eg. $value['foo']) because I think it looks a bit neater, but it's purely an aesthetic thing at that point.

Really, though, I'm hugely envious if you've explored all other avenues for optimising your algorithms and data structures, your schema and database queries, and are now focusing on the near-immeasurable differences between apostrophes and quotation marks in an attempt to squeeze out an extra dribble of performance from your system. Wink[/quote]


Agreed. Honestly, if you have that many double and single quotes in your software that it's hampering rendering then I can only think of 1 thing to be the major problem....

Get rid of the 286 you are running your server on and upgrade the hardware!
#7

[eluser]Andreas Bergström[/eluser]
jedd and brianw1975: I was only asking for clarification. I don't see any reason to use the less good alternative.

Nick Husher: Thanks! Guess I'm gonna stick with double quotes then.
#8

[eluser]jedd[/eluser]
[quote author="Andreas Bergström" date="1250095547"]jedd and brianw1975: I was only asking for clarification. I don't see any reason to use the less good alternative.[/quote]

Andreas, you're absolutely right. And improving performance is a laudable goal.

All I meant was .. it's one of those things that probably comes down to personal preference, and will be heavily skewed by the way you write your code (f.e. I don't use embedded variables within quoted areas, as I mentioned), whether you prefer clean looking / consistently written code or possibly-slightly-faster code, how smart your caching system is, etc - and I suspect benchmarking the difference would be hugely tricky, given it's going to be a small component of the overall processing time, as well as a small %age of the tasks done in your average page.

Btw, I'm going to stick with double-quotes too. Smile
#9

[eluser]Andreas Bergström[/eluser]
[quote author="jedd" date="1250097327"]

Andreas, you're absolutely right. And improving performance is a laudable goal.

All I meant was .. it's one of those things that probably comes down to personal preference, and will be heavily skewed by the way you write your code (f.e. I don't use embedded variables within quoted areas, as I mentioned), whether you prefer clean looking / consistently written code or possibly-slightly-faster code, how smart your caching system is, etc - and I suspect benchmarking the difference would be hugely tricky, given it's going to be a small component of the overall processing time, as well as a small %age of the tasks done in your average page.

Btw, I'm going to stick with double-quotes too. Smile[/quote]

I agree. Smile

- - -

On the same topic, and You're probably gonna bash me for wondering this as well but.

1. while vs for?

2. if vs switch?

Now of course there are certain situations where each is the preferred option, but in some cases both functions would do it. Especially regarding the control structures (if/switch).

I'm not a newbie PHP-dev. but I've never really though about this petty optimizing before. Smile
#10

[eluser]renownedmedia[/eluser]
I vote for using whichever is most convenient / makes development quicker, as opposed to which runs faster, I mean we are using PHP aren't we?

I can type:
echo "I have $banana bananas";
faster than:
echo 'I have ', $banana, ' bananas';




Theme © iAndrew 2016 - Forum software by © MyBB