Latest Posts:

Learn What are global variables And How are they declared In PHP

Learn What are global variables And How are they declared In PHP

What are global variables?  
The global variables are that has global scope. It means it is accessible anywhere in the program codding. So it globally accessible. The group of all global variables in a codding is called global environment. In some languages these are static variable. Where these have lifetime is the entire runtime of the program. Global variable dynamically allocated at declaration time. Global variables are used to pass the information between the different block of code. PHP also has global keyword and unusual ways for global variable. The variable which declare outside the function that also have the file scope. But they are not use or accessible inside the function without using global keyword. This keyword is used to access the variable not to declare the variable. So all script on a web page clearly access it. So in HTML the global scope is windows object and it has to related or link with all global variable.
How are they declared?
The global variable is declare the outside of a function and it use inside of the function. In c,c#, c++,etc the global variable is declare outside of the main function. There is an example of declaring global variable in PHP.
<?PHP
$ a =1;
$ b =2;
Function sum()
{       global $a,$b;
$b=$a+$b;}
Sum();
Echo $b; ?>
The above script give output 3. $a and $b declaring global with the function so all reference to refer to the global version. You can declare many global variables there is no limit. There is some other way to declare a global variable in PHP. For example by using $global and static etc.
What are the problems with using global?
  1. Global variable limits your code which is used once a context. Because you can’t have two things on given page if the static is global.
  2. Secondly the debuggers are show automatically local variables while debugging a block of code. They do not show global variable without you can requesting to see value of your global variable. This is a bad matter for debugging convenience.
  3. The global variable is slow in performance as compare local variable. Because the interpreter finding global variable in last.
  4. They are the conflict with the work of two programmers if they working in a same project. If the two global variables have same name and different purpose in the same project so they confuse the programmers and an error occur in project and it is difficult to find out the error.
  5. In self documenting the global variable as not good as compare to variables that are define within the same context of object and it is clearer that what they are and who modify them when they are declared in a particular context.
  6. Global variables are best for the global state, but some state are not global and putting these state in global state limit the usefulness of your codding and potentially more problems.     
Share on Google Plus
    Blogger Comment
    Facebook Comment