Variable scope in loops

From: Date: Tue, 31 Jul 2001 21:41:22 +0000
Subject: Variable scope in loops
Groups: php.windows 
Request: Send a blank email to php-windows+get-8599@lists.php.net to get a copy of this message
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

« previous php.windows (#8599) next »