Using a variable outside a while (scope) loop

A small scope issue in PHP, I can't name the $ report variable outside the while loop. I have tried various things, including return . This does not work, the only two functions that work here are if I echo $report variable inside the loop or I print . Which I do not want to do, although it solves the problem, but I do not want random nonsense on the user's screen.

I look around the past 15 minutes and I have not seen any problems like this.

Any help would be appreciated.

 <? 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; ?> 
+7
source share
1 answer

You can try to define a variable before the loop, for example

 $report = ""; while ($row = mysql_fetch_array($result)) { $report .= "a"."b".$row["prevDOCid"]+1; } echo $report; 

Hope this helps you!

Change Use. = not + =

+10
source

All Articles