Scope problem?: Global array assignment

Ask a Question related to PHP Development, Design and Development.

  1. #1

    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


    r Guest

  2. Similar Questions and Discussions

    1. 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...
    2. #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: ...
    3. #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: ...
    4. #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...
    5. how to make a global scope array
      <20030725224852.1123.qmail@pb1.pair.com> Jack Lee: global $a;
  3. #2

    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
    Milambar Guest

  4. #3

    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

    r Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139