0

Possible Duplicate:
curly braces in string

I still don't know what it's called. Like:

$name = 'xxx';

echo "This is a string {$name}";

What do you call that operation? Concatenating a variable using {} in to a string.

Thanks!

Community
  • 1
  • 1
Loreto Gabawa Jr.
  • 1,996
  • 5
  • 21
  • 33
  • 1
    possible duplicate of [curly braces in string](http://stackoverflow.com/questions/2596837/curly-braces-in-string), [PHP Curly bracket, what's meaning in this code](http://stackoverflow.com/questions/4563728/php-curly-bracket-whats-meaning-in-this-code), – outis Mar 20 '11 at 18:40

3 Answers3

7

This is not concatenation ; this is variable interpolation ; see the Variable parsing section, in the PHP manual.


Basically, you can use any of the two following syntaxes :

echo "This is $variable";

Or :

echo "This is {$variable}";

And you'll get the same result in both cases -- except the second one allows for more complex expressions.


Concatenation would be something like this :

echo "This is my : " . $value;

Where the content of the variable $value is concatenated to the string, using the concatenation operator ..

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I've referred to this as string/variable expansion as well. Would this be correct? – Jared Farrish Mar 20 '11 at 18:41
  • By a loose definition of "concatenation" (appending one string to another) then jun's example is concatenation (as the interpolation is at the end), albeit not via the standard concat operator. – Matthew Mar 20 '11 at 18:44
  • The manual says "variable parsing" ; I generally say/hear "variable interpolation" ; https://secure.wikimedia.org/wikipedia/en/wiki/Variable_%28programming%29 seems to indicate that interpolation, substitution, and expansion are all valid terms. – Pascal MARTIN Mar 20 '11 at 18:44
2

It's often called string or variable interpolation.

Matthew
  • 47,584
  • 11
  • 86
  • 98
1

How does {} affect a MySQL Query in PHP?

Don't let the question itself throw you - this answer gives you exactly what you are looking for.

And it's not concatenating; this is concatenating:

$myvar = "This is a string ".$name; // <<< Notice I'm concatenating the variable
                                    //     using the . operator
Community
  • 1
  • 1
Jared Farrish
  • 48,585
  • 17
  • 95
  • 104