可变变量 [英] variable variables

查看:89
本文介绍了可变变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在for循环中创建变量变量?

how do i create variable variables inside a for loop?

这是循环:

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

}

在此循环中,我想为每次传递创建一个$ seat变量,但必须像这样递增.第一次通过应为$seat1 = $_POST['seat'+$aantalZitjesBestellen],下次通过应为$seat2 = $_POST['seat'+$aantalZitjesBestellen],依此类推.

inside this loop i would like to create a variable $seat for each time it passes but it has to incrementlike so. first time it passes it should be $seat1 = $_POST['seat'+$aantalZitjesBestellen], next time it passes: $seat2 = $_POST['seat'+$aantalZitjesBestellen] and so on.

所以最后应该是:

$seat1 = $_POST['seat1'];
$seat2 = $_POST['seat2'];

以此类推.

因此$ _POST的变量和内容应该是动态的.

so the variable and the content of the $_POST should be dynamic.

推荐答案

首先,除非缺少某些内容,否则我将为此使用数组.与使用数组相比,具有$seat1$seat2等变量的实用性往往要差得多,而且麻烦得多.

Firstly, I would use an array for this unless I'm missing something. Having variables like $seat1, $seat2, etc tends to have far less utility and be far more cumbersome than using an array.

话虽如此,请使用以下语法:

That being said, use this syntax:

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

最后,PHP具有将数组键提取到符号表中的内置功能: .如果extract()与未经过滤的用户输入(例如$_POST)一起使用,则可能会带来巨大的潜在安全问题,因此请谨慎使用.

Lastly, PHP has an inbuilt function for extracting array keys into the symbol table: extract(). extract() has enormous potential security problems if you use it with unfiltered user input (eg $_POST) so use with caution.

这篇关于可变变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆