Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: global variable

  1. #1
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts

    global variable

    Code:
    function statistics_exit() {
      global $user, $recent_activity;
    .....
    }
    i am a newbie of php, expect someone can explain it for me. why put the global keyword before the variable $user, $recent_activity. could i remove it? what's the difference between have the global or haven't the global

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    India
    Posts
    39
    Thanks
    0
    Thanked 2 Times in 2 Posts
    In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. This is accomplished, conveniently enough, by placing the keyword GLOBAL in front of the variable that should be recognized as global. Placing this keyword in front of an already existing variable tells PHP to use the variable having that name. Consider an example:

    <?
    $somevar = 15;
    function addit() {
    GLOBAL $somevar;
    $somevar++;
    print "Somevar is $somevar";
    }
    addit();
    ?>
    This will produce following result.

    Somevar is 16

  • #3
    Regular Coder
    Join Date
    Jul 2009
    Posts
    152
    Thanks
    8
    Thanked 0 Times in 0 Posts
    but if i want to not change the original $somevar's value, how should i do. thank you.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,048
    Thanks
    10
    Thanked 93 Times in 91 Posts
    many people will tell you that global variables are evil ... that's not entirely true but anyway, often a global is used when perhaps passing the value by reference would do the same trick...

    PHP Code:
    $somevar 15;
    function 
    addit($var) {
        
    $var++;
    }
    addit($somevar);
    echo 
    $somevar//echos 15

    /////////////pass by reference

    function additbyreference(&$var){
        
    $var++;
    }
    additbyreference($somevar);
    echo 
    $somevar//echos 16 
    why $user can not be passed as an argument in your example I do not know, one might expect to see instead..
    PHP Code:
    function statistics_exit$user$recent_activity) {
         
    //or &$user, &$recent_activity

    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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