Page 1 of 1

Global array not shared between scripts?

Posted: 08 Nov 2026 13:26
by Norn
I tested the following code in one script:

Code: Select all

    $a[] = array("cat", "dog");
    global $a[];
    text $a[0];
Then, in another script:

Code: Select all

    global $a[];
    text $a[0];
However, $a[0] is undefined in the second script.

According to the documentation, the global command is supported by arrays:
The global command is supported by arrays, eg: global $a[];
Am I misunderstanding how global arrays work, or is this expected behavior?

Re: Global array not shared between scripts?

Posted: 09 Nov 2026 05:22
by jupe
Looks like a bug to me, topic moved.

@Don: If I modify as below then I get a proper err9 dialog too:

Code: Select all

"1"
  global $a[];
  $a[] = array("cat", "dog");
  text $a[0];
  sub "_2";
"_2"
  global $a[];
  text $a[0];

Re: Global array not shared between scripts?

Posted: 11 Nov 2026 14:28
by altoclef
Defining a variable as local and then as global should hide the original local variable. Help says (extraneous details removed):

Code: Select all

msg $foo;   // $foo (uninitialized, not a variable)
 $foo = 15;  // set local value: new local var $foo is created
 msg $foo;   // 15 (local)
 // now make $foo global
 // it's set to 8 (as previously set in script1)
 global $foo;
 msg $foo;   // 8 (global)
A simple example:

Code: Select all

$i = 5;
    global $i; // resets $i to empty
so the surprise to me is not that the global array is blank in the second script but that it isn't in the first script.

(Edited for typos)

Re: Global array not shared between scripts?

Posted: 11 Nov 2026 14:52
by altoclef
In jupe's code the array is not recognised as global once it has been populated, so the array in script "_2" is a different variable.
Screenshot 2026-11-11 134951.png

Re: Global array not shared between scripts?

Posted: 11 Nov 2026 15:20
by altoclef
As an aside, we can say:

Code: Select all

global $x = 99;
but not

Code: Select all

global $a[] = array("cat", "dog");   // Call to undefined function: array
Is that intended?

Re: Global array not shared between scripts?

Posted: 11 Nov 2026 18:42
by altoclef
Alternatively, use a function.

Code: Select all

"1"
  $a[] = array("cat", "dog");
  FooBar($a);
  text "Script 1: $a[0] $a[1] $a[2]";

function FooBar(&$array)  // must be passed by reference
{
  text "FooBar: $array[0] $array[1]";
  $array[2] = "mouse";
}


Re: Global array not shared between scripts?

Posted: 12 Nov 2026 09:29
by admin
Should be fixed in next beta.

Re: Global array not shared between scripts?

Posted: 13 Nov 2026 05:10
by jupe
My last repro looks fixed in .0628+, but a couple other things I noticed, the arrays are never marked as [global] in the Variable Stack dialog, should they be?

Also there still seems some issue, eg. below is another repro that gets a err9.

Code: Select all

  global $a[];
  explode($a[], "cat|dog");
  e $a[0]; 
Additionally, should this work in a single call when using SC array?:

Code: Select all

global $a[] = array("cat", "dog");   // Call to undefined function: array

Re: Global array not shared between scripts?

Posted: 13 Nov 2026 10:36
by admin
All confirmed, fixed the first two. :tup:

Yes, global $a[] = array("cat", "dog"); should work, but that's not supported currently. Must do global $a[]; $a[] = array("cat", "dog");.