0

What is better to use in PHP? ' or "? for something like this:

<?=$json["response"]["players"][0]["personaname"];?>

And this:

$steamid = "";
RuuddR
  • 941
  • 4
  • 13
  • 25
  • I really have good experience with this rule of thumb: for literal strings used as technical identifiers use a single quote, for human readable text phrases use double quotes. – arkascha Feb 01 '16 at 20:52
  • Okay, that's à nice way to remember that, thanks – RuuddR Feb 01 '16 at 21:19

2 Answers2

2

"Better" really depends on your use cases. In both your example, the single quote is better for the perfomance because you have no $variable interpolation needed.

Otherwise "better" may be a question of style (and a little of performance).

Read the documentation for more : http://php.net/manual/en/language.types.string.php

Mat
  • 2,134
  • 1
  • 18
  • 21
  • I'll read the documentation, thank you. – RuuddR Feb 01 '16 at 20:51
  • In modern versions of PHP there is practically no difference in performance anymore between single and double quotes. – Charlotte Dunois Feb 01 '16 at 22:18
  • Yes I agree. But that's still the reason no to use double quotes everywhere I think. By the way, if you *really* want performance improvements, use the most modern php engine, switch language, or create a C php extension. Or learn how to reduce algorithm complexity. There are always many ways for improvements. – Mat Feb 02 '16 at 10:17
1

Better? Well, let's just say that an empty string with single quotes is the most basic string literal you can use. In your example shown, I would use single quoted strings for array elements and the empty string. PHP will know that the string literal is only a string literal, and that nothing else needs to be "interpolated." Better? Who knows.

One place to use "" is with escape sequences such as "\n" .

Anthony Rutledge
  • 6,980
  • 2
  • 39
  • 44