0

The title may be a little confusing. This is my problem:

I know you can hold a variable name in another variable and then read the content of the first variable. This is what I mean:

$variable = "hello"
$variableholder = 'variable'
echo $$variableholder;

That would print: "hello". Now, I've got a problem with this:

$somearray = array("name"=>"hello");
$variableholder = "somearray['name']"; //or $variableholder = 'somearray[\'name\']';
echo $$variableholder;

That gives me a PHP error (it says $somearray['name'] is an undefined variable). Can you tell me if this is possible and I'm doing something wrong; or this if this is plain impossible, can you give me another solution to do something similar?

Thanks in advance.

1

2 Answers 2

1

For the moment, I could only think of something like this:

<?php 
    // literal are simple
    $literal = "Hello";
    $vv = "literal";
    echo $$vv . "\n";
    // prints "Hello"


    // for containers it's not so simple anymore
    $container = array("Hello" => "World");
    $vv = "container";

    $reniatnoc = $$vv;
    echo $reniatnoc["Hello"] . "\n";
    // prints "World"
 ?>

The problem here is that (quoting from php: access array value on the fly):

the Grammar of the PHP language only allows subscript notation on the end of variable expressions and not expressions in general, which is how it works in most other languages.

Would PHP allow the subscript notation anywhere, one could write this more dense as

echo $$vv["Hello"]

Side note: I guess using variable variables isn't that sane to use in production.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for the answer. Unfortunately it doesn't work with my current problem (I should have talk about it in my question, but I wanted the question to be more generic). What I have is this: $variableholder = ($id!=FALSE)?'container[\''.$id.'\']':'container'; And I need it that way because if $id is FALSE then container is just and array (if not is an associative array). Don't know if I made it clear enough.
I see. Disclaimer: I'm no PHP pro. But I've never seen variable variables until today (I have used PHP on and off for maybe seven years). My first impression was: wow, this is possible with PHP? As I suspected, this language feature made it into the list of code smells - so just as a sidenode, maybe you should not rely on such constructs at all (at least in a production setting).
Thanks a lot once again. Yeah, I chose to make an IF sentence and repeat the code with the difference that if $id is FALSE the variable has no index; if is not, it has. I just wanted to make it more elegant. Thanks a lot for the advice.
1

How about this? (NOTE: variable variables are as bad as goto)

$variablename = 'array';
$key = 'index';

echo $$variablename[$key];

1 Comment

Ah, good point. You'd need to assign $$variablename to a $foobar first. But variable variables are such awful practice anyway that this conversation shouldn't exist :/

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.