Search code examples
phpvariablesloopspostvariable-variables

Variable variables with $_POST


I have a long list of variables with a number at the end. For example address1 and I have all the way up top address14. I want to post these from a form but rather than type out $address1 = $_POST[address1] I would like to create a loop that loops round 14 times and changes the number at the end of address in both the variable name and in the $_POST section...

I am struggling to do this. I have a loop that creates the varaibles, but I keep getting errors as it isn't doing the $_POST bit.

Can someone please assist? Thank you.

This is what I currently have:

        $x = 0;
        while($x < 14) {
            $address = "address" . $x;
            $address = $$address;

            $string = "<p>Address$x:" . $address[0] . "</p>";
            echo $string;
            $x = $x + 1;

}


Solution

  • why you don't do:

    for ($i=0; $i < 14; $i++) {
        $address[$i] = $_POST['address'.$i];
    }