PHP - for() 内的变量或变量数组

发布于 2024-10-03 12:43:10 字数 762 浏览 3 评论 0原文

我有这段代码可以用 php 生成 HTML 表:

<?php 
include("numbers2.php");

echo '<table border="1">';
 echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($a1_pos_txt !== TRUE) {

echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php 
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>
<?php } }?>

我需要做的是修改 $a1_post_txt 变量,以便当 foo 循环时我将得到它而不是 $a1_pos_txt > 每次:

$a1_pos_txt
.
.
$a9_pos_txt

我基本上就是我对 $numbers["a" 所做的事情。 $i . "_pos"]name="a' . $i . '_post" 但现在该变量位于另一个变量内,我不知道如何执行此操作。

我希望它足够清楚,如果没有,请询​​问任何需要的澄清。

提前致谢!!

I have this code that generates an HTML table with php:

<?php 
include("numbers2.php");

echo '<table border="1">';
 echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($a1_pos_txt !== TRUE) {

echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php 
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>
<?php } }?>

What I need to do is modify the $a1_post_txt variable so that it when the foor loops I will get instead of $a1_pos_txt every time:

$a1_pos_txt
.
.
$a9_pos_txt

I it basically what I did with $numbers["a" . $i . "_pos"] and with name="a' . $i . '_post" but now that the variable is inside another variable I don´t know how to do this.

I hope it is clear enough, if no please ask for any clarifications needed.

Thanks in advance!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

少女的英雄梦 2024-10-10 12:43:10

使用数组代替变量。在你的数组中将包含像 true 或 flase 这样的值,这些值之前在 $a1_pos_txt......$a9_pos_txt

$arrOfValues[1] = TRUE;
$arrOfValues[2] = FALSE;
 ......
 .....
 ...
$arrOfValues[9] = TRUE;

所以代码将如下所示

<?php 
include("numbers2.php");

 echo '<table border="1">';
  echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($arrOfValues[$i] !== TRUE) {

 echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php  
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>

Instead of variable use array. In your array will contain values like true or flase, which were earlier in $a1_pos_txt......$a9_pos_txt

$arrOfValues[1] = TRUE;
$arrOfValues[2] = FALSE;
 ......
 .....
 ...
$arrOfValues[9] = TRUE;

So code will look like this

<?php 
include("numbers2.php");

 echo '<table border="1">';
  echo '<tr>';

for ($i = 1; $i <= 9; $i++) {
if($arrOfValues[$i] !== TRUE) {

 echo "<td>" . $numbers["a" . $i . "_pos"] . "</td>";

} else {?> 
<?php  
echo '<td><input type="text" name="a' . $i . '_post" size="1" maxlength="1" /></td>';
?>

七禾 2024-10-10 12:43:10

有人建议使用变量——它们太糟糕了!不要使用它们! (它们会使您的代码非常难以阅读和维护,并且有可能引入安全问题)。

其他人建议使用 eval() ——绝对不要使用它!! (几乎在所有可能的情况下,使用eval都被认为是非常糟糕的做法;它极有可能引入安全问题)

一些人建议使用数组而不是命名变量——这是正确的解决方案。

您已经使用 $numbers 完成了此操作,那么您可以对 post 变量执行类似的操作吗?

如果它们与您拥有 name="a' . $i . '_post" 的 HTML 代码相关,那么您可以更改此代码以发布变量 - 像这样:

name="a_post['.$i.']"

然后拥有名为 a1_posta2_post 等的 post 变量,您将拥有名为 a_post[1]a_post[2]< /code> 等。然后循环它们就变得非常容易,因为它们是一个数组。

Someone suggested variable variable -- they're awful! Don't use them! (they can make your code very hard to read and maintain, and have the potential to introduce security issues).

Someone else suggested using eval() -- definitely don't use that!! (using eval is considered very poor practice in virtually every possible situation; it is highly likely to introduce security issues)

Several people have suggested using an array instead of named variables -- this is the correct solution.

You already did it with $numbers, so could you do something similar with the post variables?

If they're related to the HTML code you've got name="a' . $i . '_post" then you could change this code to post variables instead -- something like this:

name="a_post['.$i.']"

Then instead of having post variables named a1_post and a2_post, etc, you will have ones named a_post[1] and a_post[2] etc. It then becomes very easy to loop through them because they're an array.

若相惜即相离 2024-10-10 12:43:10

您可以使用动态变量:

<?php
    $test = 'a';
    echo ${'test'}; 
?>

这将显示“a”。

因此,您可以将变量名称构建为字符串并获取其值!

You can use dynamic variables:

<?php
    $test = 'a';
    echo ${'test'}; 
?>

This will display "a".

So you can build a variable name as string and get its value!

新雨望断虹 2024-10-10 12:43:10

http://php.net/manual/en/function.eval.php

eval 函数将执行您想要的操作。

http://php.net/manual/en/function.eval.php

The eval function will do what you want.

行雁书 2024-10-10 12:43:10

我想你应该用数组替换你的单个变量。
我的意思是,而不是创建

$a1_pos_txt
...
$a9_pos_txt

你应该有一个数组,你可以在其中使用类似的东西:

if($myVar[$i] !== true) {...}

你在哪里以及如何初始化你的标志变量?

查理

I guess you should replace your single variable with an array.
I mean, instead of creating

$a1_pos_txt
...
$a9_pos_txt

you should have an array where you can use something like:

if($myVar[$i] !== true) {...}

Where and how are you initializing your flag variables?

Charlie

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文