Scope problem?: Global array assignment

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'...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-29-2004
r
 
Posts: n/a
Default Scope problem?: Global array assignment

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


Reply With Quote
  #2 (permalink)  
Old 04-29-2004
Milambar
 
Posts: n/a
Default Re: Scope problem?: Global array assignment

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
Reply With Quote
  #3 (permalink)  
Old 04-29-2004
r
 
Posts: n/a
Default Re: Scope problem?: Global array assignment

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



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:17 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0