What is faster in PHP, single or double quotes? [duplicate]
Possible Duplicate:
Is there a performance benefit single quote vs double quote in php?
Which is faster, single or double quotes and why?
Also, what is the advantage of using either method?
I would say that single quotes are faster because they don't require Shift ;)
The different quotes have implications on variable output and escape characters. Content inside single quotes is taken as is, no escape characters and variables are interpreted. Double quotes interpret variable values and escape special characters like newlines (\n
).
Depends what you want to do. I just did some benchmarks and tested string assignment with a 5 test cases: double quotes + variable, double quotes, double quotes and string append, single quotes, and single quotes with string append.
My test code. A million loops. String assignment.
<?php
$start = microtime(true);
$str = "";
for($i = 0; $i<1000000; $i++)
{
$str = "hello $i";
}
$end = microtime(true);
echo $end - $start;
Results:
Single and Double quotes strings without variables are equally as fast. (each echoed around .08). Single and Double quote string with variable concatenation are about the same, but slower than no variable. (each echoed around .17-.20) Double quotes with a variable in the string was slowest (around .20-.25)
So Single/Double doesn't really matter, but it seems string concatenation is faster than variable replacement.
Are you handling 1000s of strings per second? If not, you shouldn't really be too concerned.
Use double quotes if you want to use string interpolation (with variables, math, etc.)
You should also try and be consistent.
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.
Donald Knuth
Have a look at comparison of performance of double (") and single (') quotes for strings @ phpbench.
Conclusion: "In today's versions of PHP it looks like this argument has been satisfied on both sides of the line. Lets all join together in harmony in this one!"
As for speed, the only right answer is it shouldn't be your concern at all. Period. The difference, if you even find one, doesn't matter at all. To be concerned of speed, one should understand whole picture, not one pixel of it. Making your application faster and more efficient is a great and complicated task. But it cannot be done by asking "which is faster" questions. If you really concerned in that, start from learning what profiling is.
As for advantages, a manual page explains is the best place to learn it: http://php.net/types.string
It's not really advantages but rather use cases though.
If no processing of the text within is required then single is faster.
single quote is generally faster, and everything quoted inside treated as plain string,
like
echo 'anyting else ? $something';
>> anything else? $something
PHP won't use additional processing to interpret what is inside the single quote
However, compare to double quote, PHP will replace the $something will its assigned value
$something = 'yup';
echo "anyting else ? $something";
>> anything else ? yup
精彩评论