The scope of a variable is the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
- local
- global
- static
Local variables are only accessible within the function or method in which they are defined. They are useful when you want to perform calculations or store temporary data that is only needed within a specific function or method. Declaring variables with local scope can also prevent naming conflicts with variables declared outside of the function or method.
Static variables, like local variables, are only accessible within the function or method in which they are defined. However, their value persists even after the function or method has completed execution. This means that they can be used to keep track of data between function calls. It is important to note that static variables must be initialized with an initial value before their first use.
In conclusion, understanding variable scope in PHP is essential for writing efficient, readable, and maintainable code. By declaring variables with the appropriate scope, you can ensure that your code behaves as intended and is easy to read and maintain. When using global variables, it is important to be mindful of potential naming conflicts and unexpected behavior. Using local and static variables can help prevent these issues and make your code more robust.
Global and Local Scope
The concept of scope refers to the visibility or accessibility of a variable, function, or other programming construct within a program. The two main types of scopes in programming are global and local.
Global scope refers to the area of the program where a variable or function is accessible from anywhere in the program. In other words, a global variable or function can be accessed from any part of the program, including within functions or methods. Global variables and functions are typically declared outside of any functions or methods, and their value or behavior is visible throughout the program.
One potential issue with global variables and functions is that they can be modified by any part of the program, which can lead to unexpected behavior or bugs. Additionally, naming conflicts can occur when multiple variables or functions with the same name are declared in different parts of the program. To avoid these issues, it is generally recommended to minimize the use of global variables and functions and instead use local variables and functions whenever possible.
Local scope, on the other hand, refers to the area of the program where a variable or function is accessible only within a specific function or method. Local variables and functions are typically declared within the scope of a function or method, and their value or behavior is only visible within that function or method. This can help prevent naming conflicts and make the code more modular and maintainable.
One of the benefits of using local variables and functions is that they can be optimized for performance, as they do not need to be accessed from multiple parts of the program. Additionally, local variables and functions can help prevent errors by restricting the visibility and accessibility of variables and functions to only where they are needed.
In conclusion, understanding the differences between global and local scope in programming is essential for writing efficient, readable, and maintainable code. While global variables and functions can be useful in some cases, it is generally recommended to minimize their use and instead use local variables and functions whenever possible. By using local scope, programmers can help prevent errors and improve the performance, modularity, and maintainability of their code.
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function:
Example
Variable with global scope:

A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

You can have local variables with the same name in different functions, because local variables are only recognized by the function in which they are declared.
PHP The global Keyword
The global keyword is used to access a global variable from within a function or method. By default, variables declared within a function have local scope, which means they can only be accessed within that function. However, there are situations where it is necessary to access a variable that is declared outside of the function, such as a global variable.
To access a global variable from within a function, you must first declare the variable as global using the global keyword. This tells PHP to use the global variable instead of creating a new local variable with the same name. Here's an example:
$global_var = "Hello, world!";
function my_function() {
global $global_var;
echo $global_var;
}
my_function(); // Output: Hello, world!
In this example, the $global_var variable is declared outside of the my_function() function, so it has global scope. Inside the function, we use the global keyword to tell PHP that we want to access the global $global_var variable, rather than creating a new local variable with the same name.
It's important to note that using global variables can make your code harder to read and maintain, as it can be difficult to track where a variable is being used and modified. Global variables can also be modified by any part of the program, which can lead to unexpected behavior or bugs.
In general, it's best to avoid using global variables whenever possible and instead pass variables as arguments to functions or use object-oriented programming techniques to encapsulate data within objects. However, there are situations where using global variables can be useful, such as when working with legacy code or when there is a need for a variable to be accessible from multiple parts of the program.
Another important point to keep in mind when using the global keyword is that it only works within the same PHP file. If you want to access a global variable from a different file, you will need to use a different method, such as using the include or require statement to include the file that declares the global variable.
In conclusion, the global keyword in PHP is used to access a global variable from within a function or method. While it can be useful in some cases, it is generally recommended to avoid using global variables whenever possible and instead pass variables as arguments to functions or use object-oriented programming techniques to encapsulate data within objects. When using global variables, it's important to be mindful of potential naming conflicts and unexpected behavior.
The global keyword is used to access a global variable from within a function.
To do this, use the global keyword before the variables (inside the function):

PHP also stores all global variables in an array called $GLOBALS[index]. The index holds the name of the variable. This array is also accessible from within functions and can be used to update global variables directly.
The example above can be rewritten like this:

PHP The static Keyword
Normally, when a function is completed/executed, all of its variables are deleted. However, sometimes we want a local variable NOT to be deleted. We need it for a further job.
To do this, use the static keyword when you first declare the variable:

Then, each time the function is called, that variable will still have the information it contained from the last time the function was called.
Note: The variable is still local to the function.