Ask a Question related to PHP Development, Design and Development.
-
r #1
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
r Guest
-
Problem with sessions (in global scope vs class scope)
Hello, i'me having a wierd problems with sessions. PHP 4.3.3, Register globals is on, and the sessions module is installed. if i have a page like... -
#26173 [Opn->Bgs]: Global vs function scope problem?
ID: 26173 User updated by: myle34 at hotmail dot com Reported By: myle34 at hotmail dot com -Status: Open +Status: ... -
#26173 [Opn]: Global vs function scope problem?
ID: 26173 User updated by: myle34 at hotmail dot com Reported By: myle34 at hotmail dot com Status: Open Bug Type: ... -
#26173 [NEW]: Global vs function scope problem?
From: myle34 at hotmail dot com Operating system: Windows XP Home PHP version: 5.0.0b2 (beta2) PHP Bug Type: Variables... -
how to make a global scope array
<20030725224852.1123.qmail@pb1.pair.com> Jack Lee: global $a; -
Milambar #2
Re: Scope problem?: Global array assignment
r wrote:
Both:> 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
>
>
-----------------------------------------------
<?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
Milambar Guest
-
r #3
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:
>> Both:> > 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
> >
> >
> -----------------------------------------------
> <?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
r Guest



Reply With Quote

