0

Possible Duplicate:
curly braces in string

Just came across this piece of code and it got me curious...

$msg .= "–{$mime_boundary}–n";   

The $mime_boundary var was specified earlier to be output as a string.

Did a quick test....

$var = "XmytextX";
$str ="some wrapper{$var}end of";
echo $str; 

and it does indeed output into the string. I've just never seen this way of outputting a var before.

Couldn't find any documentation on it - can anyone enlighten me?

Community
  • 1
  • 1
Doug McK
  • 468
  • 5
  • 15
  • 1
    Think of how PHP will see your "quick test" if `{}` wasn't used: `...wrapper$varend of`. If you don't have a variable named `$varend`, you'll just get `... wrapper of`. – Marc B Jul 06 '11 at 14:38
  • this question has been answered on another post. The link is below: click the following link: http://stackoverflow.com/questions/2596837/curly-braces-in-string –  Jul 06 '11 at 14:36

7 Answers7

5

So, normally, you could use the double quotes to output any variable like so:

echo "Hello, $name";

But, what if you wanted to output an item inside an array?

echo "Hello, $row['name']";

That wouldn't work. So, you would enclose the variable in curly brackets to tell the compiler to interpolate the entire variable:

echo "Hello, {$row['name']}";

On that note, you could also use it with objects:

echo "Hello, {$row->name}";

Hope that helps!

Jesse Bunch
  • 6,651
  • 4
  • 36
  • 59
  • Learn something new everyday! Much appreciated and many thanks to all the other respondents too (this was an easy one eh?) – Doug McK Jul 06 '11 at 14:53
  • I think this answer explains it better: http://stackoverflow.com/questions/2596837/curly-braces-in-string/2596838#2596838 – hakre Jul 06 '11 at 15:18
4

It's called variable-interpolation. In fact you don't need the {} around the var at all. This would also work:

echo "The value of my var is $var";

However if you need a more complex variable to output it sometimes only works with the {}. For example:

echo "This is a {$very['long']['and']['complex']['variable']}";

Also note, that variable-interpolation only works in strings with double-quotes! So this wouldn't work:

echo 'this is my $var';
// outputs: this is my $var
Fidi
  • 5,754
  • 1
  • 18
  • 25
3

The curly braces set a variable name off from the rest of the text, allowing you to avoid the use of spaces.

For example, if you removed the curly braces, the PHP engine would not recognize $mime_boundary as a variable in the following statement:

$msg .= "–$mime_boundary–n";

By encapsulating the variable in curly braces, you tell the PHP engine to process the variable and return its value.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
2

It's there to eliminate ambiguity: in your case, if you wrote "some wrapper $vared of", it's clear that PHP will try to put there the value of $vared, and that's not what you want.

Gabi Purcaru
  • 30,940
  • 9
  • 79
  • 95
2

The curly braces let you specify which part of the string should be interpolated.

Consider $str ="some wrapper{$var}end of"; verses $str ="some wrapper$varend of";

They also allow you to insert array elements and class variables directly into strings

$str = "foobar $foo['bar']"; // parse error
$str = "foobar {$foo['bar']}";
$str = "this is my {$foo->bar[1]}";
Rob
  • 8,042
  • 3
  • 35
  • 37
1

It's related to variable interpolation.

This is explained in the manual (under "Complex (curly) syntax"). I'm curious as to why you haven't read it if you are working in PHP.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I've never used PHP that way outputting variables. Tbh I don't especially like this method - I prefer to use . concatenation as I can then clearly see what variables I have where – Doug McK Jul 06 '11 at 15:06
  • @Doug: I also prefer concatenation, mainly for the same reason. I don't mind curly syntax as much as the laziest method of interpolation (`"lol $cats hi mom!!1"`), as it's more explicit and my syntax highlighter can deal with it. – Lightness Races in Orbit Jul 06 '11 at 15:32
1

If you take a look at http://php.net/manual/en/language.types.string.php you'll see its just another way of embedding a variable within a string, it just allows you to define where the end of the variable is.

For example:

$var = "Animal"
echo "The {$var}s"; //Outputs "The Animals"
Jack Murdoch
  • 2,864
  • 1
  • 18
  • 27