5

this is more of a better-practice type of question.

I'd like to know if using brackets in double-quoted strings for variables is good practice.

For example:

<?php
$Variable = 'a variable';
$SingleQuotedString = 'Single quoted string with ' . $Variable;
// Single quoted string with a variable

$DoubleQuotedString = "Double quoted string with $Variable";
// Double quoted string with a variable

$DoubleQuotedStringWithBrackets = "Double quoted string with {$Variable} in brackets.";
// Double quoted string with a variable in brackets.
?>

It doesn't change the output or the code from simple tests, and obviously works. I'm just confused because not many people do this, and I don't see recommendations or people disagreeing with it, and I've been using them just fine.

Thanks for any feedback!

Toxic
  • 113
  • 1
  • 9
  • possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – JavaCake Dec 29 '13 at 23:54
  • How? None of their answers seemed fitting, yes I saw some with brackets, but other than that, nobody went exactly in-depth with what I really want to know. – Toxic Dec 29 '13 at 23:55

5 Answers5

13

The curly braces are to allow the use of arrays and objects, i.e:

$string = "my array value: {$foo['bar']}";

or

$string = "my object value: {$foo->bar}";
Martin
  • 6,632
  • 4
  • 25
  • 28
5

Fastest and cleanest version is the first one. Variables in a double quoted string... just don't "feel" right to me.

$SingleQuotedString = 'Single quoted string with ' . $Variable;

The only situation where it comes in handy I can think of is when you have a

$string = "with a {$load} of {$variables} in {one} {sentence}!";

and the readability would suffer to much otherwise.

Markus Kottländer
  • 8,228
  • 4
  • 37
  • 61
  • I don't blame you, that's why I used the brackets instead of just adding the variable in the string itself. Thanks :) – Toxic Dec 30 '13 at 00:02
  • 1
    That's a personal preference. When you have to echo every other word as a string, you'll prefer interpolation. – Jessica Dec 30 '13 at 00:04
  • 1
    This microoptimation meme holds true for ancient PHP versions. Not that it matters unless you had a billion string concatenations in your code, or actually ever see it in the profiler compared to loops or SQL queries. – mario Dec 30 '13 at 00:10
  • 2
    Well... I once worked at a company where developed an auction software which heavily relied on performance. We even couldn't use includes. :P – Markus Kottländer Dec 30 '13 at 00:13
  • "not in the old days" anymore or not, it's always good practice to do the little, easy things, like using ' instead of " when you don't need PHP to parse the string for variable subs. – Zarathuztra Dec 30 '13 at 00:14
  • 1
    @Zarazthuztra unless its at the cost of something more valuable, like readability. – Ezequiel Muns Dec 30 '13 at 00:43
  • It's easier to gloss over "This is such an $coolVar day that I wish I could blah blah" and miss the sub than 'This such an'.$coolVar.' day that I wish I could blah blah'. It also takes all of no time at all to just hit ' instead of " when you don't even need concats in the first place. – Zarathuztra Dec 30 '13 at 13:46
  • Not sure why this is the accepted answer, it doesn't answer the question nor are feelings particularly relevant. – Martin Jan 01 '14 at 15:43
3

You need to use brackets in case you have no space after your variable

$a = 1;
$aa = 2;
echo "$aaa"; // prints nothing but a notice cause $aaa is not defined
echo "{$a}aa"; // prints 1aa
echo "{$aa}a"; // prints 2a

or if you want to call an object method

echo "{$myObject->myMethod()}"; // fatal error cause $myObject is null ;) otherwise it works just fine

Otherwise you can use brackets or not.

  • Single quotes are faster if you have no variables in your string.
  • Accessing array value or object propertie does'nt require brackets. But maybe it's a bit easyer to read.
toph
  • 190
  • 1
  • 4
2

The real reason that bracket quoted variables in strings exists is for accessing values in arrays or objects. E.g.

echo "The result is {$res['foo']}"

or

echo "The result is {$res->foo}"

which won't work if you didn't use the brackets. If you find it easier to see bracket quoted strings then use them. If not then use them only when required (to dereference an array or object).

Kris Erickson
  • 33,454
  • 26
  • 120
  • 175
  • I usually never needed to call array values, or objects until I started working with DOMDocument. Thanks for your feedback :) – Toxic Dec 30 '13 at 00:04
2

These are all stylistic choices because so long as you follow the rules you'll end up with the same result. You're trading off readability, editability, error-resistance and a truly tiny amount of speed with each one. There are no accepted 'best practices', but simply developer preferences which people will defend zealously.

I find the single quote form noisy, more verbose and less aesthetically pleasing. I tend to use it only for short strings.

I prefer double quotes for longer strings, because they give me the flexibility to move interpolations around the string in an less error prone way. I use brackets when I must be explicit or need the value inside an array or object, but always err on the side of succinctness.

In general it's probably best to just be consistent to maximize team productivity.

Ezequiel Muns
  • 7,492
  • 33
  • 57