[英]what's wrong with this piece of php code?
I am trying to put an indeterminate amount of names into variable values using the loop statement: 我正在尝试使用loop语句将数量不确定的名称放入变量值中:
while ($row = mysql_fetch_row($resultdq2))
{
$name[$i] = $row[2];
$parent_ID = $row[1];
$i++;
}
Then i am trying to echo the names using the following structure/ 然后我尝试使用以下结构来回显名称/
$x = 0;
while (isset ($name[$x]))
echo $name[$x];
$x++;
The only problem is, this second loop structure freezes my pc onload, thus i am assuming that it is looping infinitely. 唯一的问题是,第二个循环结构冻结了我的pc onload,因此我假设它无限循环。
Why is it doing such? 为什么这样做呢? assuming that
$x
and $i
are the same value (ie 7) then $name[$x]
and name[$i]
should be equivalent, is that not correct? 假设
$x
和$i
的值相同(即7),那么$name[$x]
和name[$i]
应该相等,这是不正确的?
Then once my $name[$x]
condition in my second loop passes the point of variables assigned, shouldnt the loop terminate? 然后,一旦我的第二个循环中的
$name[$x]
条件通过分配的变量的点,循环是否应该终止?
Is it possible that for the loop statement condition, it is using the global variable declaration $x = 0
instead of the internal $x
value to evaluate if the condition is met, thus the statement is always true? 对于循环语句条件,是否有可能使用全局变量声明
$x = 0
而不是内部$x
值来评估条件是否满足,因此该语句始终为true?
What is the problem, and how can i correct it? 有什么问题,我该如何解决?
Maybe add '{' and '}' to increase the counter? 也许添加'{'和'}'来增加计数器?
while (isset ($name[$x])) {
echo $name[$x];
$x++;
}
Better use foreach
, add is_array
check. 更好地使用
foreach
,添加is_array
检查。 Write nice code! 写好代码!
Only echo $name[$x];
只
echo $name[$x];
is inside while loop. 在while循环内。 It should be:
它应该是:
$x = 0;
while (isset ($name[$x]))
{
echo $name[$x];
$x++;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.