Re: switch-statement overrides use of global arrays?
| From: | Henrik Hudson | Date: | Sun, 28 Oct 2001 21:52:29 +0000 |
| Subject: | Re: switch-statement overrides use of global arrays? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-72641@lists.php.net to get a copy of this message | ||
Hey-
I would think you would need to define $array_test outside in a "global"
environment rather then inside the function since I think/thought it will
just stay scoped in there and that "global" was used to access variables
which weren't defined inside a functions scope and make it look outside.
In other words
//Define GLOBAL
$array_test = array();
switch(){
BLAH
BLAH
}
function main() {
global $array_test;
BLAH
BLAH
}
function test_one(){
global $array_test;
BLAH
BLAH
}
Hope that helps.
Henrik
On Sunday 28 October 2001 15:17, Imar de Vries wrote:
> 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);
> }
--
Henrik Hudson
lists@rhavenn.net