skip to Main Content

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.

2

Answers


  1. $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.

    Login or Signup to reply.
  2. 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]);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search