在 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;
希望对你有帮助!
编辑使用 .= not +=
Edit Use .= not +=
这篇关于在 while 循环(范围)之外使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!