我可以在PHP中使用生成的变量名吗?

时间:2022-08-27 17:46:51

I have a bunch a values I would like to add together which are entered into a form. Right now, the form has 11 lines but it could get larger in the future. I can easily add all the values together with something like:

我有一堆我想要加在一起的值,这些值被输入到一个表单中。目前,该表格有11行,但未来可能会变大。我可以轻松地将所有值添加到以下内容中:

$total = $value1 + $value2 + ... + $value11;

All the values I want to add together are coming from an HTML form. I want to avoid javascript.

我想要添加的所有值都来自HTML表单。我想避免使用javascript。

But, I want to avoid having to manually do it, especially if it grows much larger. This is my attempt at adding all the values together using a loop but it returns an "undefined variable" error (it is just some test code to try out the idea):

但是,我想避免手动操作,特别是如果它变得更大。这是我尝试使用循环将所有值添加到一起但它返回一个“未定义变量”错误(它只是一些测试代码来尝试这个想法):

<?php

$tempTotal = 0;

$pBalance1 = 5;
$pBalance2 = 5;
$pBalance3 = 5;

for  ($i = 1 ; $i <= 3 ; $i++){
    $tempTotal = $tempTotal + $pBalance.$i;
}


echo $tempTotal;

?>

Is what I want to do possible in PHP?

我想用PHP做什么?

9 个解决方案

#1


7  

for  ($i = 1 ; $i <= 3 ; $i++){
    $varName = "pBalance".$i;
    $tempTotal += $$varName;
}

This will do what you want. However you might indeed consider using an array for this kind of thing.

这将做你想要的。但是你可能会考虑使用数组来做这种事情。

#2


7  

I would use @unexist's solution.. give the input fields names like:

我会使用@expoist的解决方案..给输入字段命名如下:

<input name="myInput[]" />
<input name="myInput[]" />
<input name="myInput[]" />
...

Then in your PHP get the sum like this:

然后在你的PHP中得到这样的总和:

$total = array_sum($_REQUEST['myInput']);

Because of the '[]' at the end of each input name, PHP will make $_REQUEST['myInput'] automatically be an array. A handy feature of PHP!

由于每个输入名称末尾的'[]',PHP将使$ _REQUEST ['myInput']自动成为一个数组。 PHP的一个方便功能!

#3


3  

Uhm why don't you use an array? If you give the forms a name like foobar[] it will be an array in PHP.

嗯,你为什么不使用阵列?如果你给表单命名像foobar []那么它将是PHP中的一个数组。

#4


2  

The values you need are posted from a form, right? If so, you could iterate through the keys in the $_POST variable that match your form field's generated names.

您需要的值是从表单中发布的,对吧?如果是这样,您可以遍历$ _POST变量中与表单字段生成的名称匹配的键。

foreach($_POST as $key=>$value)
{
  if(strpos($key, 'pBalance')===0)
  {
    $final_total += $value;
  }
}

#5


2  

You can use an array to store your data, and just loop over it.

您可以使用数组来存储数据,然后循环遍历它。

$tempTotal = 0;

$balances[] = 5;
$balances[] = 5;
$balances[] = 5;

for  ($i = 0; $i <= count($balances); $i++) {
    $tempTotal = $tempTotal + $balances[$i];
}

Or for brevity, use a foreach loop:

或者为了简洁起见,使用foreach循环:

foreach($balances as $balance) {
    $tempTotal += $balance;
}

#6


2  

If you're trying to collect the values of a POST, you should really use an array. You can avoid having to manually piece together such an array by using:

如果您尝试收集POST的值,则应该使用数组。您可以避免使用以下方法手动拼凑这样的阵列:

<input type="text" name="vals[]" value="one" />
<input type="text" name="vals[]" value="two" />

$_POST["vals"] will then be array("one", "two");

$ _POST [“vals”]将是数组(“一”,“两个”);

#7


1  

In about 99% of all cases involving a PHP generated variable, you are doing The Wrong Thing. I'll just re-iterate what others have said:

在大约99%的涉及PHP生成变量的案例中,你正在做错误的事情。我只是重复其他人所说的话:

USE AN ARRAY

使用阵列

#8


0  

Try

$varName = 'pBalance' . $i;
$tempTotal = $tempTotal + $$varName;

#9


-1  

The concept you're looking for is called a variable variable (at least it's called that in PHP). Here is the official documentation and a useful tutorial. The syntax for a variable variable is a double dollar sign ($$).

您正在寻找的概念称为变量变量(至少在PHP中称为变量变量)。这是官方文档和有用的教程。变量变量的语法是双美元符号($$)。

#1


7  

for  ($i = 1 ; $i <= 3 ; $i++){
    $varName = "pBalance".$i;
    $tempTotal += $$varName;
}

This will do what you want. However you might indeed consider using an array for this kind of thing.

这将做你想要的。但是你可能会考虑使用数组来做这种事情。

#2


7  

I would use @unexist's solution.. give the input fields names like:

我会使用@expoist的解决方案..给输入字段命名如下:

<input name="myInput[]" />
<input name="myInput[]" />
<input name="myInput[]" />
...

Then in your PHP get the sum like this:

然后在你的PHP中得到这样的总和:

$total = array_sum($_REQUEST['myInput']);

Because of the '[]' at the end of each input name, PHP will make $_REQUEST['myInput'] automatically be an array. A handy feature of PHP!

由于每个输入名称末尾的'[]',PHP将使$ _REQUEST ['myInput']自动成为一个数组。 PHP的一个方便功能!

#3


3  

Uhm why don't you use an array? If you give the forms a name like foobar[] it will be an array in PHP.

嗯,你为什么不使用阵列?如果你给表单命名像foobar []那么它将是PHP中的一个数组。

#4


2  

The values you need are posted from a form, right? If so, you could iterate through the keys in the $_POST variable that match your form field's generated names.

您需要的值是从表单中发布的,对吧?如果是这样,您可以遍历$ _POST变量中与表单字段生成的名称匹配的键。

foreach($_POST as $key=>$value)
{
  if(strpos($key, 'pBalance')===0)
  {
    $final_total += $value;
  }
}

#5


2  

You can use an array to store your data, and just loop over it.

您可以使用数组来存储数据,然后循环遍历它。

$tempTotal = 0;

$balances[] = 5;
$balances[] = 5;
$balances[] = 5;

for  ($i = 0; $i <= count($balances); $i++) {
    $tempTotal = $tempTotal + $balances[$i];
}

Or for brevity, use a foreach loop:

或者为了简洁起见,使用foreach循环:

foreach($balances as $balance) {
    $tempTotal += $balance;
}

#6


2  

If you're trying to collect the values of a POST, you should really use an array. You can avoid having to manually piece together such an array by using:

如果您尝试收集POST的值,则应该使用数组。您可以避免使用以下方法手动拼凑这样的阵列:

<input type="text" name="vals[]" value="one" />
<input type="text" name="vals[]" value="two" />

$_POST["vals"] will then be array("one", "two");

$ _POST [“vals”]将是数组(“一”,“两个”);

#7


1  

In about 99% of all cases involving a PHP generated variable, you are doing The Wrong Thing. I'll just re-iterate what others have said:

在大约99%的涉及PHP生成变量的案例中,你正在做错误的事情。我只是重复其他人所说的话:

USE AN ARRAY

使用阵列

#8


0  

Try

$varName = 'pBalance' . $i;
$tempTotal = $tempTotal + $$varName;

#9


-1  

The concept you're looking for is called a variable variable (at least it's called that in PHP). Here is the official documentation and a useful tutorial. The syntax for a variable variable is a double dollar sign ($$).

您正在寻找的概念称为变量变量(至少在PHP中称为变量变量)。这是官方文档和有用的教程。变量变量的语法是双美元符号($$)。