This is a discussion on Scope problem?: Global array assignment within the alt.comp.lang.php forums, part of the PHP Programming Forums category; How can I make an array global and make assignments to it from within my functions? for example: this doesn'...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
How can I make an array global and make assignments to it from within my
functions? for example: this doesn't work (displays nothing): <?php $mArray=array(); mMain1; function mMain1 () { global $mArray; $mArray["test1"]="Hello"; $mArray["test2"]=" World!"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>A test</title> </head> <body> <?= $mArray["test1"] . $mArray["test2"] ?> </body> </html> but this does work (displays "Hello World!): <?php $mArray=array(); $mArray["test1"]="Hello"; $mArray["test2"]=" World!"; mMain1; function mMain1 () { global $mArray; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>A test</title> </head> <body> <?= $mArray["test1"] . $mArray["test2"] ?> </body> </html> Thanks, R |
|
|||
|
r wrote:
> How can I make an array global and make assignments to it from within my > functions? > for example: this doesn't work (displays nothing): > > <?php > $mArray=array(); > mMain1; > function mMain1 () > { > global $mArray; > $mArray["test1"]="Hello"; > $mArray["test2"]=" World!"; > } > ?> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> > <head> > <title>A test</title> > </head> > <body> > <?= $mArray["test1"] . $mArray["test2"] ?> > </body> > </html> > > > but this does work (displays "Hello World!): > > > <?php > $mArray=array(); > $mArray["test1"]="Hello"; > $mArray["test2"]=" World!"; > mMain1; > > function mMain1 () > { > global $mArray; > } > ?> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> > <head> > <title>A test</title> > </head> > <body> > <?= $mArray["test1"] . $mArray["test2"] ?> > </body> > </html> > > > Thanks, > R > > Both: ----------------------------------------------- <?php $mArray=array(); mMain1(); function mMain1() { global $mArray; $mArray["test1"] = "Hello"; $mArray["test2"] = "Earth"; } echo "$mArray[test1] $mArray[test2]<br>"; ?> ----------------------------------------------- and: ----------------------------------------------- <?php $mArray=array(); mMain1(); function mMain1() { global $mArray; $mArray["test1"] = "Hello"; $mArray["test2"] = "Earth"; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>A test</title> </head> <body> <?= "$mArray[test1] $mArray[test2]"; ?> </body> </html> ---------------------------------------------- Work fine for me. Is there any error generated at all? Also try looking in /var/log/httpd/error_log |
|
|||
|
Yours does work...hmmm...my problem is the call to mMain1!!! No
parentheses. Changed to mMain1 () and works fine. That's another bad habit I picked up from VB. Thanks R "Milambar" <milambar@milambar.com> wrote in message news:V6ckc.8$9A3.2@newsfe6-win... > r wrote: > > > How can I make an array global and make assignments to it from within my > > functions? > > for example: this doesn't work (displays nothing): > > > > <?php > > $mArray=array(); > > mMain1; > > function mMain1 () > > { > > global $mArray; > > $mArray["test1"]="Hello"; > > $mArray["test2"]=" World!"; > > } > > ?> > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > > "http://www.w3.org/TR/html4/loose.dtd"> > > <html> > > <head> > > <title>A test</title> > > </head> > > <body> > > <?= $mArray["test1"] . $mArray["test2"] ?> > > </body> > > </html> > > > > > > but this does work (displays "Hello World!): > > > > > > <?php > > $mArray=array(); > > $mArray["test1"]="Hello"; > > $mArray["test2"]=" World!"; > > mMain1; > > > > function mMain1 () > > { > > global $mArray; > > } > > ?> > > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > > "http://www.w3.org/TR/html4/loose.dtd"> > > <html> > > <head> > > <title>A test</title> > > </head> > > <body> > > <?= $mArray["test1"] . $mArray["test2"] ?> > > </body> > > </html> > > > > > > Thanks, > > R > > > > > Both: > ----------------------------------------------- > <?php > $mArray=array(); > > mMain1(); > function mMain1() { > global $mArray; > $mArray["test1"] = "Hello"; > $mArray["test2"] = "Earth"; > } > echo "$mArray[test1] $mArray[test2]<br>"; > ?> > ----------------------------------------------- > and: > ----------------------------------------------- > <?php > $mArray=array(); > > mMain1(); > function mMain1() { > global $mArray; > $mArray["test1"] = "Hello"; > $mArray["test2"] = "Earth"; > } > ?> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" > "http://www.w3.org/TR/html4/loose.dtd"> > <html> > <head> > <title>A test</title> > </head> > <body> > <?= "$mArray[test1] $mArray[test2]"; ?> > </body> > </html> > ---------------------------------------------- > > Work fine for me. > > Is there any error generated at all? Also try looking in > /var/log/httpd/error_log |