PHP - for() 内的变量或变量数组
我有这段代码可以用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用数组代替变量。在你的数组中将包含像 true 或 flase 这样的值,这些值之前在 $a1_pos_txt......$a9_pos_txt
所以代码将如下所示
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
So code will look like this
有人建议使用变量——它们太糟糕了!不要使用它们! (它们会使您的代码非常难以阅读和维护,并且有可能引入安全问题)。
其他人建议使用
eval()
——绝对不要使用它!! (几乎在所有可能的情况下,使用eval
都被认为是非常糟糕的做法;它极有可能引入安全问题)一些人建议使用数组而不是命名变量——这是正确的解决方案。
您已经使用
$numbers
完成了此操作,那么您可以对 post 变量执行类似的操作吗?如果它们与您拥有
name="a' . $i . '_post"
的 HTML 代码相关,那么您可以更改此代码以发布变量 - 像这样:然后拥有名为
a1_post
和a2_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!! (usingeval
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:Then instead of having post variables named
a1_post
anda2_post
, etc, you will have ones nameda_post[1]
anda_post[2]
etc. It then becomes very easy to loop through them because they're an array.您可以使用动态变量:
这将显示“a”。
因此,您可以将变量名称构建为字符串并获取其值!
You can use dynamic variables:
This will display "a".
So you can build a variable name as string and get its value!
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.
我想你应该用数组替换你的单个变量。
我的意思是,而不是创建
$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
https://www.php.net/manual/en/language。 Variables.variable.php
变量变量会做你想做的事。
https://www.php.net/manual/en/language.variables.variable.php
Variable Variables will do what you want..