1

I have a FOR that runs the following line:

        for ($x=1;$x<=10;$x++) 
    {
        $result=$question[$x]."<br /><b>Correct Answer: ".$question[$x]_answer."</b><br />";
        echo $resultado;
    } 

If I change the $question[$x]_answer to $answer[x] for example, everything works. So my question is, why doesn't it work if I have it with the underscore after the [x]. The part about [x]_answer. Is there a problem with underscore after having a [x] in a variable?

Machavity
  • 30,841
  • 27
  • 92
  • 100
Luis Alvarado
  • 8,837
  • 12
  • 46
  • 60
  • 1
    What would you expect `$question[$x]_answer` to be? What is your array called? – kba Aug 14 '12 at 01:15

3 Answers3

3

$question[$x] refers to an array with $x being the index.

i.e:

$question[0] = "How old are you?";
$question[1] = "Male or Female?";

The way you have it, $question[$x]_answer is not a valid variable, the index must come at the end. You cannot have [ or ] in a variable name.

Maybe try renaming your variable to $question_answer[$x] if you have an array of answers.

i.e:

$question_answer[0] = "12";
$question_answer[1] = "Male";

or you can use a variable variable if you hard-named your variables with the index already: i.e:

$question_0_answer = "12";
$question_1_answer = "Male"

For this, you would use: $question_{$x}_answer

David Houde
  • 4,835
  • 1
  • 20
  • 29
3

Its a perfectly valid question. I don't see any reason for the downvotes.

This is what you would use: ${$question[$x].'_answer'}.

Let me explain with an example,

$a[1] = 'user';
$b = 1;

So,

$a[$b] == 'user'; //true

And if

$user_ans = 'success';

We can use that like

${$a[$b].'_ans'} == 'success'; //true

As @DavidHoude pointed out in the comment, this is an example of Variable Variables

abhshkdz
  • 6,335
  • 1
  • 21
  • 31
  • +1 for this answer. This is a variable variable, http://php.net/manual/en/language.variables.variable.php – David Houde Aug 14 '12 at 01:26
  • *What advantage does using variable-variables have?* Almost every other language has decided there are *better ways* to solve this problem .. so thus if there is *no good advantage* (for task X), why use variable-variables (for task X)? In this case I would argue that they *are not appropriate* to cleanly approach/handle this problem. –  Aug 14 '12 at 01:28
  • Exactly. I was just answering the OP's question. – abhshkdz Aug 14 '12 at 01:30
  • (I'm not downvoting this, of course, because it answers the question, but *why* do it like this?) –  Aug 14 '12 at 01:30
  • Many thanks. Maybe I was in sleep state but did not notice the array symbols there. – Luis Alvarado Aug 14 '12 at 01:40
0

_ has no special meaning in php and hence is just like any other standard character. Your logic is identical to any of the following

$question[$x]Qanswer
$question[$x]Aanswer
etc...

None of these are valid because a variable cannot use [, ], or $(these are special).

e.g., you can't have variables like $my[var or $my$var.

So $question[$x]_answer either looks like a single variable to php but with invalid tokens in it(the [$x]) or it looks like an array, $question[$x] with some weird thing attached to it(like a space is missing or dot).

e.g.,

$question[$x] answer
$question[$x].answer

look like more valid things(the last one is probably what you want).

AbstractDissonance
  • 1
  • 2
  • 16
  • 31