0

This may be a very basic question, but I have struggled to find a suitable answer. And my first question, so please be gentle.

When combining strings with variables, I understand that enclosing variables within a single-quoted string will not expand the variable, whereas double quoted strings will expand the variable (and other special characters), giving rise to the syntax in the two examples:

$animal1='brown fox';
$animal2='lazy dog';
echo "The quick $animal1 jumps over the $animal2";
echo 'The quick '.$animal1.' jumps over the '.$animal2;

I recall reading that PHP parses single quote encapsulated strings faster than double quoted encapsulated strings, because it is not spending time looking for variables that it needs to resolve. Is this true? If so, is this gain lost when concatenating a string and a variable, as in the second example?

My main question is: When working with strings and variables, as in the above examples, is either way of encapsulating preferable?

long
  • 218
  • 7
  • 16
  • I agree that this is likely a duplicate - which I did not come across when searching, but @hakre produces a valid reference which is newer than the previous question. Thanks all. – long Oct 16 '13 at 10:53

4 Answers4

5

I recall reading that PHP parses single quote encapsulated strings faster than double quoted encapsulated strings, because it is not spending time looking for variables that it needs to resolve. Is this true?

No, this is wrong, see Disproving the Single Quotes Performance Myth (Jan 2012; By Nikic)

My main question is: When working with strings and variables, as in the above examples, is either way of encapsulating preferable?

That is only a matter of taste, find your way and don't get distracted by misguiding and false information you find online. Write the way you can read and edit it well.

kero
  • 10,647
  • 5
  • 41
  • 51
hakre
  • 193,403
  • 52
  • 435
  • 836
  • 2
    Do you have any data to prove your statement? – Cobra_Fast Oct 16 '13 at 10:32
  • 3
    Sure, this is perfectly documented (see [Disproving the Single Quotes Performance Myth (Jan 2012; By Nikic)](http://nikic.github.io/2012/01/09/Disproving-the-Single-Quotes-Performance-Myth.html)). But you best metric that on your own, so the learning effect is higher. – hakre Oct 16 '13 at 10:32
  • @hakre Good to know, thanks for link. But I can't change vote unless you edit your answer (that's what SO said). – Elon Than Oct 16 '13 at 10:39
  • Thank you for the edit, that article was a pretty solid read. – Cobra_Fast Oct 16 '13 at 10:59
  • 1
    After getting so much critic for my answer I started to guess that it makes no difference as the parser will need to iterate over the whole code regardless if it is a double or single quoted string. (sounds logic, now ;).. +1 for @Nikic :) – hek2mgl Oct 16 '13 at 11:07
1

It is very slightly faster to use ' instead of " in general. This difference is so small though in most PHP applications you would never notice the difference. In regards to the concatenation, both are about similar speeds, there is very little difference between them, read benchmarks here: https://stackoverflow.com/a/1813685/2859624

Community
  • 1
  • 1
Adam Rivers
  • 1,065
  • 7
  • 13
0

Although there is only minimal time difference (Something that e.g., is negligable compared to server-client communication times), one way makes for a much clearer text. The latter makes it A LOT easier for you to spot variables inserted into text.

I'd always take the variables out of regular text, as in example 2 you give.

Jelle Ferwerda
  • 1,254
  • 1
  • 7
  • 13
-1

Here's some benchmark that you can refer to:

http://www.phpbench.com/

Look for the single quotes vs double quotes part. The difference is so small that unless you're going to do it a zillion times, there's not going to be a performance impact at all.

There is one more way you can encapsulate variables, useful especially when you're working with arrays:

$str = "Encapsulating an array {$array['key']}";
TheOnly92
  • 1,723
  • 2
  • 17
  • 25
  • The measurements done that website (phpbench.com) are highly questionable and have been criticized to great extend. The (most likely a male) person running it never really responded to the criticism which stands for itself. Don't quote on questionable sources. – hakre Oct 16 '13 at 10:34
  • Uhm well then please quote a trustable source stating that the site is untrusted. I haven't seen such criticism elsewhere. – TheOnly92 Oct 16 '13 at 10:37
  • Well, it's perhaps better for you to remain blindfolded then. If you don't want to, learn about how to metric first. Even if you learn only one hour about that and then compare how that boy does it, you will just see yourself, I don't need to point you to the criticism that were given in concrete (which I must admit I don't have at hand, just remembering from top of my head, but that site *has* been criticized and the reasons were valid). – hakre Oct 16 '13 at 10:40