variable variables

后端 未结 5 1070
醉话见心
醉话见心 2020-11-28 16:54

how do i create variable variables inside a for loop?

this is the loop:

for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {

}         


        
5条回答
  •  生来不讨喜
    2020-11-28 17:41

    (Expanded for clarity - you may be able to do a one-liner)

    for ( $counter = 1; $counter <= $aantalZitjesBestellen; $counter ++) {
        $varname = 'seat' . $counter;
        $$varname = $POST[$varname];
    }
    

    BUT! You really shouldn't do this. (And if you really must, see cletus' answer for the built-in PHP way to do it - this is considered bad practice too, though.)

    Reconsider your problem and see if arrays might be the solution (I guess it will). This will make both inspection (via e.g. var_dump()) and iteration easier and does not pollute the global variable space.

提交回复
热议问题