0

A problem with variable variables in PHP.

$y = "_POST[\"131rde" . $x . "box\"]";
if ($$y == "yes"){DO SOME STUFF;}

Seems to be failing. Details below.

I have been taking in data from my site with this:

$rde1 = $_POST["131rde1box"];
$rde2 = $_POST["131rde2box"];
$rde3 = $_POST["131rde3box"];
$rde4 = $_POST["131rde4box"];
$rde5 = $_POST["131rde5box"];
$rde6 = $_POST["131rde6box"];
$rde7 = $_POST["131rde7box"];
$rde8 = $_POST["131rde8box"];
$rde9 = $_POST["131rde9box"];

Each post either contains nothing or the string "yes". I have many such inputs so I was trying to speed up like with variable variables.

for ($x = 1; $x <= count($rdeex); $x++) {
   $y = "rde" . $x;
   if ($$y == "yes"){array_push($chosenquestions,$rdeex[$x-1]);}
}

The above works absolutely fine.

I have tried to speed up to get rid of the horrible repeated lines at the top with:

for ($x = 1; $x <= count($rdeex); $x++) {
    $y = "_POST[\"131rde" . $x . "box\"]";
    if ($$y == "yes"){array_push($chosenquestions,$rdeex[$x-1]);}
}

But it fails. And I've no idea why. The string "_POST["131rde" . $x . "box"]" seems to be fine.

I'm something of a newbie and recognise that the above is pretty poor style, but wondering just why it fails at the moment. Thank you.

JSto
  • 11
  • 2
  • Would you try for ($x = 1; $x <= count($rdeex); $x++) { $y = "_POST[\"131rde" . $x . "box\"]"; // Instead of using $$y, use the following: if ($_POST["131rde" . $x . "box"] == "yes") { array_push($chosenquestions, $rdeex[$x - 1]); } } By directly accessing the $_POST array with the constructed variable name $_POST["131rde" . $x . "box"], you can achieve the desired functionality without using variable variables. This way, you avoid the potential pitfalls of variable variables and make your code more readable and maintainable. – Damian Chudobiński Jul 25 '23 at 18:14
  • 1
    What do you mean exactly, when you say "But it fails"? Also, some additional debugging information would help… like have you checked whether the lines inside the `for` loop are even being executed? It also seems like we would need to see how `$rdeex` is set, to understand what your code would be expected to do. – SimonMayer Jul 25 '23 at 19:12
  • 1
    Why not just name the elements `131rdebox[]`? Then you don't need to deal with this at all. Just iterate over `131rdebox`. – user3783243 Jul 25 '23 at 19:17
  • Yeah, if you ever find yourself defining variables like `$var1 = 'something1';`, `$var2 = 'something2';`, stop and rethink your approach. An Array exists for this purpose; a single variable with `key/value` pairs. In PHP, keys can be numeric or strings. The `$_POST` super global variable is an Array, with each input being a key. A simple `for($i = 1; $i <= 9; $i++)` loop, then accessing `$_POST["131rde{$i}box"]` (using string interpolation, or `$_POST["131rde" . $i . "box"]` which is string concatenation) is all you need here. – Tim Lewis Jul 25 '23 at 19:19

2 Answers2

2
$y = "rde" . $x;
($$y == "yes");

You're falling into the variable variables trap here. While this works, it most often is not what you want because it is error prone and complicated. (Only because something works must not mean it should be used.)

$rde1 = $_POST["131rde1box"];
$y = $_POST["131rde" . $x . "box"];

This is not variable variables any longer, it is just an associative array and you use a string as the key.

And it works within the loop already.

And if it helps you with the readability, you can also use string interpolation:

$y = $_POST["131rde{$x}box"];

Works with double-quoted ("...") strings in PHP. The variable $x is expanded to its string value ("131rde1box" given $x = 1).


wondering just why it fails at the moment

It fails so that you ask on SO about it to learn that you should not use variable variables. Apart from that, the reason is that only the variable name can be variable in variable variables, not the array access. But just don't.

Learn about arrays, they work much better for repetition and for code in general.

And you can use them for form fields, too.

hakre
  • 193,403
  • 52
  • 435
  • 836
1

First: What exactly is the $rdeex variable in your example? It would be helpful if we had the full context.

But it fails. And I've no idea why. The string "_POST["131rde" . $x . "box"]" seems to be fine.

_POST["131rde" . $x . "box"] is not the name of the variable. That would be _POST. The ["131rde" . $x . "box"] part is NOT a part of the variable name. In your example you would have to do

for ($x = 1; $x <= count($rdeex); $x++) {
    $y = "_POST";
    if ($$y["131rde" . $x . "box"] == "yes"){
        array_push($chosenquestions,$rdeex[$x-1]);
    }
}

That said, variable variables are generally something you'll want to avoid as it is very prone to errors and rarely actually needed. You might notice that the fixed snippet above actually doesn't need a variable variable at all. Something a little nicer might be...

for ($x = 1; $x <= count($rdeex); $x++) {
    $key = "131rde" . $x . "box";
    if (isset($_POST[$key]) && $_POST[$key] === "yes") {
        array_push($chosenquestions,$rdeex[$x-1]);
    }
}
Mike
  • 95
  • 8