Jump to content

[SOLVED] Curly braces round variables


graham23s

Recommended Posts

Hi Guys,

 

i thought i was in a pretty good routine when typing my code but now everywhere i see this:

 

{$variable}

 

when i normally do just:

 

$variable

 

can anyone tell me why this needs done? and should i make it a rule of thumb to do it also?

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/67956-solved-curly-braces-round-variables/
Share on other sites

It depends.

 

Inside of double quotes, using curly braces just makes it more clear to the PHP parser which part of the string is the variable and which part is the string:

// Assume a variable named $prefix
echo "That is $prefixtacular!";
// The PHP parser would look for a variable named $prefixtacular and most likely not find it
echo "That is {$prefix}tacular!";
// No confusion in the above statement

 

The second use of curly brackets concerning variables is for declaring or using a variable with a dynamic name.

// Assume a variable named $name holding the value "bob"
${$name} = "Hello";

The statement above will create a variable named $bob and assign it a value of "Hello"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.