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
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"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.