how to access global array (user defined)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

how to access global array (user defined)

Post by tawfiq »

Hi,

I wonder if someone can help me understand this. I can access global variable from inside the function but not a global array. How do I add value to my array then?

Code: Select all

 
<?
$attributes[] = "";   // a blank array
$optionCounter=0;   // a global variable
 
 
function setOption($a){
 
global $optionCounter;  //accessing global variable - works fine
global $attributes // accessing global array - doesn't work
//add attribute to attribute array
$attributes[] = $a;  // this doesn't work either coz access to global array failed
 
//increment optionCounter by one
$optionCounter++;  // works fine
        
    
}
//test time
setOption("Size");
?>
 
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: how to access global array (user defined)

Post by JAM »

Change the functino abit. What does this tell you? At the bottom...

Code: Select all

function setOption($a) {
    print_r($GLOBALS);
}
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

Re: how to access global array (user defined)

Post by tawfiq »

No Output at all
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: how to access global array (user defined)

Post by JAM »

Perhaps the use was unclear...

Code: Select all

<pre><?php
    error_reporting(E_ALL);
    $foo = array();
    $bar = 0;
    function setOption($a) {
        print_r($GLOBALS);
    }
    setOption();
?></pre>
That shows nothing?!
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: how to access global array (user defined)

Post by JAM »

Sorry, double post because the forum got fubared for some reason yesterday...
Last edited by JAM on Mon Jan 28, 2008 5:19 am, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: how to access global array (user defined)

Post by Kieran Huggins »

there's a semicolon missing in your original example - is it there in your test code?
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

Re: how to access global array (user defined)

Post by tawfiq »

I have solved it :)

Thanks for your help.

Here is the code, I think changing from
$attributes[] = ""; to $attributes = array(); did the job

Code: Select all

 
 
//create an array with no element to store all the attributes (i.e options)
$attributes = array();
 
// ************ function to set option for product entry ***********
function setOption($a){
 
global $attributes;
 
//insert an element to the global array
$attributes[] = $a;
 
}
 
setOption("Size");
setOption("Colour");
 
 
Post Reply