473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How do I "global" an array?

moishy
104 100+
I have a function which adds strings to an array, but outside the function it didn't add the string to the array.

Example:

[PHP]function AddStringToArray($name,$string) {

$theArray[$name] = $string;

print "inside the function: ".$theArray[$name]."<p>";
}

AddStringToArray('abc','hello world!');

print "outside the function: ".$theArray['abc'];

{[/PHP]

The above will result:

inside the function: hello world!

outside the function:
Nov 17 '06 #1
4 67149
ronverdonk
4,258 Expert 4TB
As follows;
[php]<?php
$theArray = array();
function AddStringToArray($name,$string) {
global $theArray;
$theArray[$name] = $string;
print "inside the function: ".$theArray[$name]."<p>";
}
AddStringToArray('abc','hello world!');
print "outside the function: ".$theArray['abc'];
?>[/php]
Ronald :cool:
Nov 17 '06 #2
Velhari
46
I have a function which adds strings to an array, but outside the function it didn't add the string to the array.

Example:

[PHP]function AddStringToArray($name,$string) {

$theArray[$name] = $string;

print "inside the function: ".$theArray[$name]."<p>";
}

AddStringToArray('abc','hello world!');

print "outside the function: ".$theArray['abc'];

{[/PHP]

The above will result:

inside the function: hello world!

outside the function:
Hi,
You can access the arrays from outside the function by declaring the array as global.

I changed your code as follows to rectify your problem.... you can use this code to acheive your goal....

[PHP]function AddStringToArray($name,$string)
{
global $thestring;
$thestring[$name]=$string;
echo "Inside Function:".$thestring[$name]."<br>";
}
AddStringToArray('abc','hello world');
echo "Outside Function:".$thestring['abc'];[/PHP]

With Regards,
Velmurugan.H
Nov 17 '06 #3
moishy
104 100+
But it doesn't work if I call from another function:

[PHP]
// Whatever was said before...

function LoopArray() {

foreach ($theArray as $value){
// Do whatever...
}
[/PHP]

will output:
Warning: Invalid argument supplied for foreach()

-even though i GLOBALed the array!
Nov 17 '06 #4
ronverdonk
4,258 Expert 4TB
Yes it does! You must declare the array in your main program and declare it global in all functions that use it.

Ronald :cool:
Nov 17 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: mrbog | last post by:
I have an array/hash that stores path information for my app. As in, what directory this is in, what directory that's in, what the name of the site is, what the products are called, etc. It's...
1
by: Chris Stromberger | last post by:
This doesn't seem like it should behave as it does without using "global d" in mod(). d = {} def mod(): d = 3 mod() print d
7
by: Lyn | last post by:
Hi and Season's Greetings to all. I have a question regarding the use of a qualifier word "Global". I cannot find any reference to this in Access help, nor in books or on the Internet. "Global"...
9
by: Javaman59 | last post by:
I saw in a recent post the :: operator used to reach the global namespace, as in global::MyNamespace I hadn't seen this before, so looked it up in MSDN, which explained it nicely. My question...
4
by: BB | last post by:
Hello all, I might be missing something here, but am trying to understand the difference between using application-level variables--i.e. Application("MyVar")--and global variables--i.e. public...
3
by: Pierre | last post by:
Hello, In an aspx page (mypage.aspx) from a web projet, I would like to get the value of a variable of the projet that is declared as public in a module. The variable can be called from...
2
by: Bryan | last post by:
I am currently working on a project that I am interested in porting to Win32 or .NET, but I don't know where to get started. I have quite a bit of experience with MFC, and I have been working with...
7
by: twang090 | last post by:
I find in other team member's project, they are referencing a type in following format " public static global::ErrorReport.Description Description = new global::ErrorReport.Description(); " I...
4
ChrisWang
by: ChrisWang | last post by:
Hi, I am having trouble understanding the use of 'global' variables I want to use a global variable with the same name as a parameter of a function. But I don't know how to use them at the same...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.