switch-statement overrides use of global arrays?
| From: | Imar de Vries | Date: | Sun, 28 Oct 2001 21:17:19 +0000 |
| Subject: | switch-statement overrides use of global arrays? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-72639@lists.php.net to get a copy of this message | ||
Hi all,
a very frustrating problem. For some reason I am not able to pass on an
array between two functions, although I declare it global in both of
them. I suspect my use of a switch statement to control the flow of the
functions is causing some trouble, but I'm not sure. Changing the order
in which I declare the array global did not help, nor the use of
$GLOBALS.
This is a simple representation of what I do:
switch ($action) {
default:
main();
break;
case "test_one":
test_one();
break;
}
// -------------------------------------------
function main() {
$array_test = array();
global $array_test;
$array_test[0] = "First array, first element";
$array_test[1] = "First array, second element";
print("Content of first array in the main function is now:<br>");
var_dump($array_test);
print ("<p><form method=\"post\" action=\"arraytest.php\"
enctype=\"multipart/form-data\">");
?>
<input type="hidden" name="action" value="test_one">
<input type="submit" value="Test" tabindex="6">
</form>
<?
}
// -------------------------------------------
function test_one() {
global $array_test;
// This results in NULL
print("Content of first array in the first test function is now:<br>");
var_dump($array_test);
$array_test[2] = "First array, third element";
$array_test[3] = "First array, fourth element";
// This results in the third and fourth element being shown
print("<p>After addition, content of first array is:<br>");
var_dump($array_test);
}
--
Imar de Vries - I.O.deVries@students.let.uu.nl - ICQ 6972439