2

I've been reading an PHP5 book, and the author commonly used this syntax

${"{$something}_somethingelse"};

I have no idea what that means. Does it dynamically generate a variable name?

Someone help me out?

2
  • 3
    The top voted answer is correct; However, I will say that as a long time PHP developer I rarely use these, and most of the time I consider them a code smell. Commented Jul 31, 2009 at 0:34
  • 3
    I'll help you out - buy another book. Definitely code smell to use these "commonly" Commented Jul 31, 2009 at 0:35

4 Answers 4

9

It is a language feature called Variable variables.

Consider the following piece of code:

$a = 'hello';

This is pretty straight forward. It creates the variable $a and sets its value to 'hello'.

Let's move on with:

$$a = 'world';
${$a} = 'world';

Basically, since $a = 'hello', those two statement are the equivalent of doing:

$hello = 'world';

So the following:

echo "$a ${$a}";

Is the equivalent of doing:

echo "$a $hello";

Braces { }

The braces are used to prevent ambiguity problems from occurring. Consider the following:

$$a[1] = 'hello world';

Do you want to assign a variable named after the value of $a[1] or do you want to assign the index 1 of the variable named after $a?

For the first choice, you would write it as such:

${$a[1]} = 'hello world';

For the second choice:

${$a}[1] = 'hello world';

Your example

Now, for your example.

Let's consider that:

$something = 'hello';

Using your example as such:

${"{$something}_somethingelse"} = 'php rocks';

Would essentially be equivalent of doing:

$hello_somethingelse = 'php rocks';
Sign up to request clarification or add additional context in comments.

1 Comment

Please remember that variable variables are usually a bad idea since it is usually the job of arrays (or objects) to do the job.
2

They are 'variable variables'. See this.

Comments

1

Brackets allow you to make more advanced variable names. It your Case if $something was equal to test it would be:

${"test_somethingelse"};

Which is just an advanced variable name.

Here is an example.

$test = "test";
${"test_test"} = "test2";

echo $test; // prints test
echo ${"test_test"}; // prints test2

Using Variable Varaibles, as everyone else mentioned, you can create variables based on other variables. So in your case, he was making a variable based on $something's value

$something = "test";

${"{$something}_somethingelse"};

turns into

${"test_somethingelse"};

Comments

1

That will replace the {$something} with the value of $something.

I think the inner curly braces are just for readability and to help when doing $object->property etc.

Because it seems to be also in a variable, that is called a variable variable.

For example,

$foo = 'bar';

$$foo = 7;

echo $bar;

// produces 7;

1 Comment

Hope you don't mind - I misunderstood the first time I read it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.