1
dejadingo
Smarty tag brackets -vs- curly braces in string
  • 2004/12/2 17:42

  • dejadingo

  • Just popping in

  • Posts: 71

  • Since: 2004/10/22


Can someone please explain the difference between these two constructs?

I understand the Smarty tag brackets "<{ $some_value }>"

But I also see a number of strings that appear to be used as a kind of dynamic Smarty template, which contain PHP variables simply enclosed in curly braces. I even think I came across some more complex PHP code inside curly braces.

Is this a PHP feature I don't yet know about?
When and how can this be used?

Thanks.

2
Mithrandir
Re: Smarty tag brackets -vs- curly braces in string

Where did you see the curly braces with code in between?

They are used in mail templates that are not Smarty templates, but simple texts, where the tags between { and } are replaced with dynamic content before sending the mail.

3
dejadingo
Re: Smarty tag brackets -vs- curly braces in string
  • 2004/12/3 0:54

  • dejadingo

  • Just popping in

  • Posts: 71

  • Since: 2004/10/22


I also commonly see it in header() function calls. And now I understand...
It _is_ a PHP thing I don't know about. I found this at
http://us2.php.net/language.types.string
Hope it helps someone else trying to wade into this.

-------------

Complex (curly) syntax

This isn't called complex because the syntax is complex, but because you can include complex expressions this way.

In fact, you can include any value that is in the namespace in strings with this syntax. You simply write the expression the same way as you would outside the string, and then include it in { and }. Since you can't escape '{', this syntax will only be recognised when the $ is immediately following the {. (Use "{\$" or "\{$" to get a literal "{$"). Some examples to make it clear:
<?php
// Let's show all errors
error_reporting(E_ALL);

$great 'fantastic';

// Won't work, outputs: This is { fantastic}
echo "This is { $great}";

// Works, outputs: This is fantastic
echo "This is {$great}";
echo 
"This is ${great}";

// Works
echo "This square is {$square->width}00 centimeters broad."

// Works
echo "This works: {$arr[4][3]}";

// This is wrong for the same reason as $foo[bar] is wrong 
// outside a string.  In other words, it will still work but
// because PHP first looks for a constant named foo, it will
// throw an error of level E_NOTICE (undefined constant).
echo "This is wrong: {$arr[foo][3]}"

// Works.  When using multi-dimensional arrays, always use
// braces around arrays when inside of strings
echo "This works: {$arr['foo'][3]}";

// Works.
echo "This works: " $arr['foo'][3];

echo 
"You can even write {$obj->values[3]->name}";

echo 
"This is the value of the var named $name{${$name}}";
?>

Login

Who's Online

230 user(s) are online (161 user(s) are browsing Support Forums)


Members: 0


Guests: 230


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits