Results 1 to 6 of 6

Thread: PHP global arrays in functions

  1. #1
    Join Date
    May 2008
    Location
    Ontario, Canada
    Beans
    Hidden!
    Distro
    Ubuntu

    Question PHP global arrays in functions

    Hey everyone,

    I am fairly new to PHP, and I now have a problem that has been bugging me for a while.
    Here is some example code to explain my problem.
    PHP Code:

    <?php

    $myArray 
    array('first','second','third');

    function 
    myFunct1(){
       global 
    $myArray;
       if(
    //stuff){
          
    $myArray = array("first" => "Errors Occurred, Please Try Again.");
       }
    }

    function 
    myFunct2(){
       global 
    $myArray;
       if(
    //stuff){
          
    $myArray = array("second" => "Errors Occurred, Please Try Again.");
       }
    }

    function 
    myFunct3(){
       global 
    $myArray;
       if(
    //stuff){
          
    $myArray = array("third" => "Errors Occurred, Please Try Again.");
       }
    }

    myFunct1();
    myFunct2();
    myFunct3();
    print_r($myArray);

    ?>
    What I expect to happen, is that if the IF statement is true in each case, the array should hold 3 different strings. However what it does is, only ever keep the last string written to it. Can someone explain to me why it would do this?

    If it doesn't make sense, I have attached my actual code with some examples.

    Thanks - CC7

    ## SOLVED ##

    I realized that
    PHP Code:
    $myArray = array("third" => "Errors Occurred, Please Try Again."); 
    AND
    PHP Code:
    $myArray["third"] = "Errors Occurred, Please Try Again."
    are NOT the same. The fist one (re)-creates an array. The second one updates the index!
    Last edited by vandorjw; August 24th, 2010 at 02:16 AM. Reason: SOLVED

  2. #2
    Join Date
    Jul 2008
    Beans
    1,491

    Re: PHP global arrays in functions

    You will want to read up on the global keyword. It tells PHP that a variable should be acessed from global scope rather than from local (function) scope.

  3. #3
    Join Date
    Feb 2006
    Beans
    468

    Re: PHP global arrays in functions

    HELLo!

    I don't recommend using the global keyword, for many reasons. What I'd do, in this case, is probably something like this:
    PHP Code:
    <?php
    function My_Func_1 () {
        if (
    true) {
            return 
    "Message 1";
        }
    }

    function 
    My_Func_2 () {
        if (
    true) {
            return 
    "Message 2";
        }
    }
    function 
    My_Func_3 () {
        if (
    true) {
            return 
    "Message 3";
        }
    }

    $Messages = array ();
    $Messages['first'] = My_Func_1 ();
    $Messages['second'] = My_Func_2 ();
    $Messages['third'] = My_Func_3 ();
    Though, this is a very constructed example, and if the app looks like this in real life I would probably have reconsidered some of my design calls.

    Happy codin'!

  4. #4
    Join Date
    May 2008
    Location
    Ontario, Canada
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: PHP global arrays in functions

    Hey thanks for the help with what I thought to be a solved problem.

    @Reiger - Yep I know. Without the global keyword, my functions would just create another local array, and destroy that array when the functions are done. With the Global, they use the "Global" Array instead.

    @ Hellkeepa - What is wrong or..why should I not use a global? It works fine. Could you give me a link that describes some of the drawbacks?

    Thanks - CC7

  5. #5
    Join Date
    Jan 2009
    Beans
    33
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: PHP global arrays in functions

    I would do it a little more like this:

    PHP Code:

    class Messages
    {
        public static 
    $messages = array();
        
        public static function 
    set($key$message)
        {
            
    self::$messages[$key] = $message;
        }
    }

    function 
    myFunct1()
    {
        if(
    TRUE)
        {
            
    Messages::set("first""Errors Occurred, Please Try Again.");
        }
    }

    function 
    myFunct2()
    {
        if(
    TRUE)
        {
            
    Messages::set("second""Errors Occurred, Please Try Again.");
        }
    }

    print_r(Messages::$messages);
    ?> 

  6. #6
    Join Date
    Feb 2006
    Beans
    468

    Re: PHP global arrays in functions

    HELLo!

    cc7gir: Mainly it's a readability issue, as it makes tracking what happens to the global variable that much harder. Especially in larger codebases. This, in turn, introduces a huge risk for more bugs. Bugs which can be quite troublesome to debug.
    It also may introduce a security risk, or rather amplify a security risk.

    Here's a couple of articles on the issue, and you can find more with a simple google search:
    http://www.webapper.com/blog/index.p...bal-variables/
    http://c2.com/cgi/wiki?GlobalVariablesAreBad
    http://en.wikipedia.org/wiki/Global_variable

    Xeoncross: While I might be inclined to agree that a OOP-implementation would perhaps be better in most cases you'd see something like this, I don't think it's the best option to spring upon somone who's as new as the OP seems to be.

    Happy codin'!

Bookmarks

Posting Permissions

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