在while循环(范围)之外使用变量 [英] Using a variable outside of the while loop (scope)
问题描述
关于PHP范围的小问题,我似乎无法在while循环之外调用变量$ report.我尝试了各种方法,包括return
.这不起作用,这里唯一起作用的两个函数是如果我echo
变量$report
在循环内,或者我print
它.尽管可以解决问题,但我不希望这样做,但我不想在用户的屏幕上出现乱码.
Small problem regarding scope in PHP, I can't seem to call the variable $report outside of the while loop. I have tried various things, including return
. This doesn't work, the only two functions that work here are if I echo
the variable $report
inside the loop, or if I print
it. Which I do not want to do, although it solves the problem, but I don't want random gibberish on the user's screen.
过去15分钟左右,我一直在转转,在这里我还没有看到任何类似这样的问题.
I have been looking around for the last 15 or so minutes, and I haven't seen any problems quite like this one on here.
任何帮助将不胜感激.
<?
require "functions2.php";
require "members.php";
$query = "SELECT MAX(DOCid) as prevDOCid from reports";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$prevDOCid = $row[prevDOCid];
$thisDOCid = $prevDOCid+1;
$report = "a"."b".$thisDOCid;
}
echo $report;
?>
推荐答案
您可以尝试在循环之前定义变量,例如
You could try to define the variable before the loop, e.g.
$report = "";
while ($row = mysql_fetch_array($result)) {
$report .= "a"."b".$row["prevDOCid"]+1;
}
echo $report;
希望这对您有所帮助!
编辑使用.=不是+ =
Edit Use .= not +=
这篇关于在while循环(范围)之外使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!