SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to globals work ? examples...

    I know that globals are much more secure and, well to put it bluntly, better in everyway. Will someone show me some examples of how globals work, on the same page, on multiple pages etc. I just don't understand them....

    Thanks, Chris.
    cgi://www.scriptingx.com - Everything CGI

  2. #2
    SitePoint Zealot theFinn's Avatar
    Join Date
    Oct 2000
    Location
    Edmonton
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm... If you mean global variables then you're wrong. They are not more secure, they are not better, they are simply more conviniant.

    Global variables a pain in PHP because they don't scope as you would expect if you've been programing in other languages befor.

    Here's a bit of an example:

    PHP Code:
    $local_var 100;

    function 
    my_func($var)
    {
       
    $var2 10;
       global 
    $global_var;

       echo 
    "This is a passed in variable: $var";
       echo 
    "This is a local var: $var2";
       echo 
    "This is a golbal var: $global_var";
       echo 
    "This is a local var I don't have access to: $local_var";
    }

    $global_var 200;
    my_func(300); 
    Now, in order for a variable to be global it must be defined as such, thus the line: global $global_var; in the function. Anything defined outside the function and not globled within it will not be access able, also anything in the function will not be accessable outside it. These are local variable.

    So, the above example will print
    This is a passed in variable: 300
    This is a local var: 10
    This is a global var: 200
    This is a var I don't have access to:

    I hope that made sence...but I get the suspicion you wern't really talking about golbal vars...
    - theFinn
    Creator and Project Manager, phpBB
    http://www.theFinn.net
    http://www.tarotclass.com


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
  •