Global Variables in PHP

Discussion in 'PHP' started by Complete, Feb 7, 2011.

  1. #1
    Is it possible to have true global variables in PHP? The tests I have done so far seem to suggest that you can have a global variable that is global only to the php file that declares it and not to your whole website. Once you go to another php file, the variable seems to loose what you assign it.
     
    Complete, Feb 7, 2011 IP
  2. moads

    moads Member

    Messages:
    115
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #2
    Yes, globals are set within a PHP instance... not for your entire site. You would have to use cookies or a session to retain information stored in variables.
     
    moads, Feb 7, 2011 IP
  3. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #3
    <?php
    session_start();
    $_SESSION['you_dont_loose_this_varible'] = "for setted time in php.ini";
     
    G3n3s!s, Feb 7, 2011 IP
  4. Gonzo4u

    Gonzo4u Well-Known Member

    Messages:
    410
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Hello,
    If security is not a concern, you can also use a flat file to store and retrieve data (variables) across the site, eg. a config file or xml file etc. The best way is to use cookies or sessions.

    Regards,
    Gonzo
     
    Gonzo4u, Feb 8, 2011 IP