3

I'm trying to build a dynamic variable in PHP and despite looking at a number of the questions on the matter already here on StackOverflow I'm still stumped... :/

Variable variables is something I've never quite understood - hopefully someone here can point me in the right direction. :)

$data['query']->section[${$child['id']}]->subsection[${$grandchild['id']}]->page[${$greatgrandchild['id']}] = "Fluffy Rabbit";

Onviously the above does not work but if I hard code a variable as such:

$data['query']->section[0]->subsection[3]->page[6] = "Very Fluffy Rabbit";

...then all is fine, so obviously I'm not building my dynamic variable correctly. Any ideas?

UPDATE:

Hmm, ok I should have pointed out that these are not keys in an array - I'm addressing nodes in the XML using an ID which is specified as an attribute for each node, so the XML has the following structure:

<subtitles>
<section id="0">
<subsection id="0">
<page id="1">My content that I want to write</page>
<page id="2">My content that I want to write</page>
<page id="3">My content that I want to write</page>
</subsection>
</section>
</subtitles>

Hopefully that helps explain things a little better. :)

5
  • 4
    Variable-Variables are evil and should be avoided. Especially in your case where even you when writing it can't figure it out... Imagine how hard it will be to figure out what's going on in a year when you need to fix a bug. Write readable code. Write simple code... Commented Nov 2, 2010 at 13:12
  • 1
    @ircmaxell it can simplify code and even make it more readable. for example, imagine a factory, huge switch (creates dozens of classes) and instead of that this piece of code: factory($class_name){return new $class_name()} Commented Nov 2, 2010 at 13:22
  • @Itay Moav: In this case your app will die if you add a $class_name that does not exist. Commented Nov 2, 2010 at 13:28
  • @Itay Moav: I don't consider that a variable-variable. Sure it's a very similar concept (similar to $foo($bar)), but I'm talking about $$foo or ${$foo}... Where the variables are being referenced by a name stored in another variable (the true definition of a variable-variable)... Commented Nov 2, 2010 at 13:38
  • 1
    @Stegeman that was just to show the idea, It is easy to add is_class or something similar, and have a default class. Commented Nov 2, 2010 at 20:15

5 Answers 5

6

Why do you think you need dynamic variables here? Doesn't this just do what you want:

$data['query']->section[$child['id']]->subsection[$grandchild['id']]->page[$greatgrandchild['id']] = "Fluffy Rabbit";
Sign up to request clarification or add additional context in comments.

Comments

1

In this example you don't need dynamic variables.

If $child["id"] has the value 0, $grandchild["id"] has the values 3 and $greatgrandchild["id"] has the value 6, you should use something like:

$data['query']->section[$child['id']]->subsection[$grandchild['id']]->page[$greatgrandchild['id']] = "Fluffy Rabbit";

Normally you use dynamic variables like this:

$variable = "variableName";

$$variable = "Some value";

echo $variableName;

This will display:

Some value

EDIT

Totally agree with ircmaxell

Comments

1
$foo = "hello";

$$foo = " world";

//echo $foo.$$foo;

echo $foo.$hello;

2 Comments

It is more right explanation: $foo = "hello"; $$foo = " world"; echo $foo.$hello;
Output 'hello'. Because $foo has a space.)
1

I looks like you're confusing variable variables with good old array keys. Variable variables are a mechanism that allows to read (or write) a value into a variable whose name is unknown or can change and, honestly, they're hardly ever necessary:

<?php

$first_name = 'John';
$last_name = 'Smith';

$display = 'first_name';
echo $$display; // Prints 'John';

$display = 'last_name';
echo $$display; // Prints 'Smith';

However, your code suggest that you only want to access a key inside an array:

<?php

$person = array(
    'first_name' => 'John',
    'last_name' => 'Smith',
);

$display = 'first_name';
echo $person[$display]; // Prints 'John';

$display = 'last_name';
echo $person[$display]; // Prints 'Smith';

In PHP, an array key is either an integer or a string, but it doesn't need to be a literal: you can retrieve the key from a variable.

Comments

0
$foo='bobo';
echo $foo;//"bobo"
$$foo='koko';
echo $$foo;//"koko"
echo $bobo;//"koko"

Comments

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.