Re: Variable scope in loops
| From: | Mike Flynn | Date: | Wed, 01 Aug 2001 03:43:18 +0000 |
| Subject: | Re: Variable scope in loops | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-8606@lists.php.net to get a copy of this message | ||
Hi Tom,
First problem, is that the variable $incident_array isn't declared beforehand outside the loop.
Do:
var $incident_array = array();
before the loop. That way PHP doesn't assume that it's scoping is only for within the loop, since that's where the variable is created, not having been previously created.
Also as a sidenote, I don't think you want to loop to $num_columns+1. Seems to me if there's 2 columns, say, you'll be doing.. 1, 2, 3.. which is 3 columns, which is more than the number of results. There are only $num_columns
Give some of those a try..
-Mike
At 02:41 PM 7/31/01 -0700, Tom Tsongas wrote:
Hi folks.
I have an interesting problem with regards to variable scope in loops.
Now I understand how this operates in functions but its the first time I
have seen variable scope in a loop.
Below is a code snippet of what I have:
// execute SQL query
OCIExecute($sql_statement) or die("Couldn't execute statement.");
// get number of columns for use later
$num_columns = OCINumCols($sql_statement);
// format results by row
while (OCIFetch($sql_statement)) {
for ($i = 1; $i < $num_columns+1; $i++) {
// Create array to store fetch results
$incident_array[i] = OCIResult($sql_statement,$i);
//echo "<P>Incident array value $i: $incident_array[i]</P>";
}
}
// Output results of incident array
echo "<P>First array value: $incident_array[1]</P>";
Now I 'expected' the echo statement to properly display the results of
the first array element but instead, the value is blank. Now when I
uncomment the echo statement WITHIN the for loop, it does display so I
know the values are being stored but apparantly the scope of the array
is within the loop and not outside.
How can I make the array values accessible outside of the loop? I found
a 'global' statement in the documentation but it didn't seem to like
taking an array as a parameter. I had it looking something like this:
global $incident_array[];
Any help would be appreciated.
Tom Tsongas
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-windows-unsubscribe@lists.php.net
For additional commands, e-mail: php-windows-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net
-=- Mike Flynn - Burlington, VT -=-mike@mikeflynn.net http://www.mikeflynn.net/ * Give blood * Wouldn't the free market, in its true form, be anarchy?