Can I make arguments global?

Discussion in 'PHP' started by dwest, Dec 29, 2006.

  1. #1
    Hi,
    I have a few functions that work together. Within each one, various variables are created.

    How can I access variables created in function A from with in function B?

    Is there some declaration that makes the variables from all the functions available to all the functions without changing the PHP global setting?

    Thanks!
     
    dwest, Dec 29, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    You can use the $GLOBALS variable, or you can use Classes.
     
    nico_swd, Dec 29, 2006 IP
  3. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!
    I'll look up the $Globals variable and go from there.
     
    dwest, Dec 29, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  5. KalvinB

    KalvinB Peon

    Messages:
    2,787
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #5
    If you need something from Function A then you should return it. If you need more than one variable from Funtion A then you should return an array.

    
    function A()
    {
    return 1;
    }
    
    function B()
    {
    $a = A();
    }
    
    Code (markup):
    Globals are a great way to make your program unmanagable.
     
    KalvinB, Dec 30, 2006 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Just create a php file called global_functions.php, and include this file in your header (or just make sure it's include globally). Then you can put however many "globally accessible" but still not global variables and functions sitewide :)
     
    ccoonen, Dec 30, 2006 IP
  7. Nikolas

    Nikolas Well-Known Member

    Messages:
    1,022
    Likes Received:
    22
    Best Answers:
    0
    Trophy Points:
    150
    #7
    This is another way :

    
    $a = 1;
    
    function aPlus()
    {
     global $a;
     $a++;
    }
    
    PHP:
     
    Nikolas, Dec 30, 2006 IP
  8. php_daemon

    php_daemon Active Member

    Messages:
    34
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    95
    #8
    You should seriously consider OOP :)
     
    php_daemon, Dec 30, 2006 IP