SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Enthusiast Scripting X's Avatar
    Join Date
    May 2001
    0 Post(s)
    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:// - Everything CGI

  2. #2
    SitePoint Zealot theFinn's Avatar
    Join Date
    Oct 2000
    0 Post(s)
    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;

    $var2 10;

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

    $global_var 200;
    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


Posting Permissions

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