Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何从一段时间内使用变量,但在一段时间外使用变量?_Php_Variables_While Loop - Fatal编程技术网

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循环内部获取变量,并将其拉到循环外部: