Php 如何从一段时间内使用变量,但在一段时间外使用变量?
我有这个:Php 如何从一段时间内使用变量,但在一段时间外使用变量?,php,variables,while-loop,Php,Variables,While Loop,我有这个: $nome='x.png'; if(file_exists('../../images/produtos/'.$nome)){ $i = 1; while(file_exists('../../images/produtos/'.$i."_".$nome)) { $i++; } $nome = $i."_".$nome; } 然后我需要将变量$nome放入一个mysql\u查询,就像这样 mysql_quer
$nome='x.png';
if(file_exists('../../images/produtos/'.$nome)){
$i = 1;
while(file_exists('../../images/produtos/'.$i."_".$nome))
{
$i++;
}
$nome = $i."_".$nome;
}
然后我需要将变量$nome放入一个mysql\u查询,就像这样
mysql_query("INSERT INTO produtos (nome) VALUES ('$nome)");
但它不显示“已编辑”变量。如答案中所述,您可以尝试在while循环之前定义变量。见下文:
$report = "";
while ($row = mysql_fetch_array($result)) {
$report += "a"."b".$row["prevDOCid"]+1;
}
echo $report;
下面是一个很好的示例,演示了如何在PHP中从while循环内部获取变量,并将其拉到循环外部: