1

I want $masking_x to act as $masking_y (i.e. dynamically insert $radiovalue but still act as a varibale).

My code is

while($field_radio = mysql_fetch_assoc($result_radio)) {
    $radiovalue = $field_radio[radiovalue];
    echo "Radio value: ".$radiovalue."</br>";   
    $masking = "field_masking[checkbox_1001_".$radiovalue."]";
    $masking_x = '$'.$masking;
    $masking_y = "$field_masking[checkbox_1001_2]";
    echo "Masking: ".$masking."</br>";
    echo "Masking_x: ".$masking_x."</br>";
    echo "Masking_y: ".$masking_y."</br>";
    die;

I'm getting the following output:

Radio value: 2
Masking: field_masking[checkbox_1001_2]
Masking_x: $field_masking[checkbox_1001_2]
Masking_y: 2

Any guidance would be greatly appreciated. Thanks in advance.

2
  • 1
    What are you expecting to get? Commented Jul 18, 2012 at 13:08
  • Does field_masking have to be dynamic as well? What's wrong with echo $field_masking["checkbox_1001_".$radiovalue];? Commented Jul 18, 2012 at 13:09

3 Answers 3

2

You are using variable variables slightly wrong. Try this instead:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
$masking_x = "field_masking['checkbox_1001_".$radiovalue."']";
$masking_y = "field_masking['checkbox_1001_2']";
echo "Masking_x: ".$$masking_x."</br>";
echo "Masking_y: ".$$masking_y."</br>";

The double dollar-sign must be in the code, not in the string.

It's also possible to do this:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
echo "Masking_x: ".${"field_masking['checkbox_1001_".$radiovalue."']"}."</br>";
echo "Masking_y: ".${"field_masking['checkbox_1001_2']"}."</br>";

Note also that I added single quotes into your string to properly quote the strings used for the array keys, associative array keys these are regular strings and should be quoted as such.

However it is important to note that variable variables are rarely the right solution to a given problem. It's usually possible to come up with something better using arrays or references instead.

EDIT

After playing around with it, it seems you cannot use a variable variable to reference an array key when the key definition is in the string. It's such a horrible thing to do, I'm not surprised I've never come across this limitation.

The long of the short of it is that you cannot do exactly what you are trying to do, and the best solution for you is probably to use references instead:

$radiovalue = $field_radio['radiovalue'];
echo "Radio value: ".$radiovalue."</br>";   
$masking = &$field_masking['checkbox_1001_'.$radiovalue];
echo "Masking: ".$masking."</br>";
$masking = "A different value";
echo "Masking: ".$masking."</br>";
// Original value has been updated as well
echo "\$field_masking['checkbox_1001_'.\$radiovalue]: ".$field_masking['checkbox_1001_'.$radiovalue]."</br>";
Sign up to request clarification or add additional context in comments.

3 Comments

I get undefined errors on all of those. What PHP version supports that?
@Naltharial After playing around with it, it seems you cannot use a variable variable to reference an array key when the key definition is in the string. It's such a horrible thing to do, I'm not surprised I've never come across this limitation. I will edit my answer to reflect this.
Thank you; The field_masking did not have to be dynamic so Naltharial's $field_masking["checkbox_1001_".$radiovalue]; solution solved my problem. I also learned quite a bit from the other comments and I'm gratefully for everyone's help. Thanks again.
1

If you want to dynamically access an array variable, split it in two parts, single variable won't work.

$name = 'field_masking';
$key = 'checkbox_1001_'.$radiovalue;

echo $$name[$key];

1 Comment

Thank you; The field_masking did not have to be dynamic so Naltharial's $field_masking["checkbox_1001_".$radiovalue]; solution solved my problem. I also learned quite a bit from the other comments and I'm gratefully for everyone's help. Thanks again.
0

Take a look at variable variables. Do note that this is a potential security risk if you're working with user input.

In your case: $masking_x = $$masking;

1 Comment

Thank you; The field_masking did not have to be dynamic so Naltharial's $field_masking["checkbox_1001_".$radiovalue]; solution solved my problem. I also learned quite a bit from the other comments and I'm gratefully for everyone's help. Thanks again.

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.